Java的新手,学习如何从字符串中收集用户输入。 假定该代码接受用户输入的两个单词,然后将其除以第一个单词和第二个单词。我反复收到错误消息:输入输入字符串:线程“ main” java.util.NoSuchElementException 中的异常,我对此并不了解...
//Initializations
Scanner scnr = new Scanner(System.in);
String inputString = "";
int commaPlacement = 0;
String firstWord = "";
String secondWord = "";
//Collect user input
System.out.print("Enter input string: ");
inputString = scnr.next();
commaPlacement = inputString.indexOf(",");
//Create while loop
while( (!inputString.equals("q")) ) {
//Recollect input string if there is no comma
if( (commaPlacement < 0) ) {
System.out.println("Error: No comma in string");
System.out.print("Enter input string: ");
inputString = scnr.next();
}
//Continue if there is a comma in input string
if( (commaPlacement >= 0) ) {
Scanner inSS = new Scanner(inputString);
firstWord = inSS.next();
secondWord = inSS.next();
System.out.println("First word: " + firstWord);
System.out.println("Second word: " + secondWord);
//Collect user input again
System.out.print("Enter input string: ");
inputString = scnr.next();
}
return;
}
有人可以提供帮助吗? 预先谢谢你。