java NoSuchElementException:未找到行
我正在做一个简单的项目,我从一个website项目中获得了它,这给了我一些挑战,以提高我在java中的编码技能
import java.util.Scanner;
public class Test1 {
public void test() {
Scanner in = new Scanner(System.in);
System.out.println("enter something :");
String str = in.nextLine();
StringBuilder sb = new StringBuilder(str);
if (str.isEmpty()) {
System.out.println("you should write something");
}
if(str.length()<=30){
System.out.println("reverse : "+sb.reverse());
}else {
System.out.println("Error");
}
System.out.println("----------------------");
}
public static void main(String[] args) {
Test1 c = new Test1 ();
for (int i = 1; i <= 10 ; i++) {
System.out.println("case number : " +i);
c.test();
}
}
}
case number : 1
enter something : ayoub
reverse : buoya
----------------------
...loop continue ..
我的代码在eclipse终端中的工作方式与我所希望的一样,但当我将其放入网站的“代码编辑器”中时,最后一个代码给了我一个运行时错误,该错误表示:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540)
at Test1.test(Test1.java:10)
at Test1.main(Test1.java:31)
我试图在StackOverflow上搜索一些解决方案,但没有找到
# 1 楼答案
您可能正在使用一个没有标准输入的在线java代码编辑器/编译器
正如arcy所述,您可能正在使用一个带有内置控制台窗口的IDE,该窗口允许您将标准输入传递给程序
下面的online editor将允许您添加输入。 您应该注意使用
Scanner
的nextLine
方法的方式:您得到的异常是扫描仪没有获得任何输入的结果,如here所示。我建议您使用Scanner的方法
hasNextLine
重构扫描仪上的循环