gpt4 book ai didi

java - 在命令提示符下提示用户

转载 作者:行者123 更新时间:2023-12-03 06:04:09 25 4
gpt4 key购买 nike

我目前正在开发一个程序,该程序采用Google表格中的信息并运行计算。我使用“gradle -q run”来运行程序,并希望它在终端内提示用户。现在,我正在使用扫描仪,但是在运行时(而不是等待我添加输入),它显示“输入您的名称:线程“main”中的异常java.util.NoSuchElementException”,如何解决此问题?

public static void main(String[] args) throws IOException {
// Build a new authorized API client service.
Sheets service = getSheetsService();

// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
String spreadsheetId = "1Pfs-EpysJZXibe1pHiPLEx8cOCC86H5he5ouApejqiE";
String range = "Sheet1!A:E";

// create a scanner so we can read the command-line input
Scanner scanner = new Scanner(System.in);

// prompt for the user's name
System.out.print("Enter your name: ");

// get their input as a String
String username = scanner.nextLine();

// prompt for their age
System.out.print("Enter your age: ");

// get the age as an int
int age = scanner.nextInt();

System.out.println(String.format("%s, your age is %d", username, age));


ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
if (values == null || values.size() == 0) {
System.out.println("No data found.");
} else {
System.out.println("Dominik's Awesome Script Says:");
System.out.println("");
for (List row : values) {
// Print columns A and E, which correspond to indices 0 and 4.
System.out.printf("%s, %s\n", row.get(0), row.get(4));
}
}
}

最佳答案

尝试使用

while(scanner.hasNextLine()){
username = scanner.nextline();
}

它对 https://www.compilejava.net/有用

关于java - 在命令提示符下提示用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39752962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com