感觉我在这里遗漏了一些非常基本的东西,但我的大脑现在已经被炒了。目标是用逗号解析字符串,通过一个循环并在下一行之前抛出。很确定它与userInput = scan.nextLine();
有关import java.util.Scanner;
public class ParseStrings
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String userInput = "";
boolean finished = false;
while (!finished)
{
System.out.print("Enter input string: \n");
userInput = scan.nextLine();
if (userInput.equals("q"))
{
System.out.println("First word: " + userInput);
finished = true;
} else
{
String[] userArray = userInput.split(",");
System.out.println("First word: " + userArray[0]);
System.out.println("Second word: " + userArray[1]);
System.out.println();
}
}
return;
}
}
投掷:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at ParseStrings.main(ParseStrings.java:12)
提前谢谢