gpt4 book ai didi

java - NoSuchElementException : No line found

转载 作者:行者123 更新时间:2023-11-30 12:08:03 25 4
gpt4 key购买 nike

我正在做一个简单的项目,我从 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 上搜索一些解决方案,但没有找到。

最佳答案

您可能正在使用没有标准输入的在线 Java 代码编辑器/编译器。

arcy 所述,您可能正在使用带有内置控制台窗口的 IDE,它允许您将标准输入传递给您的程序。

以下online editor将允许您添加输入。您应该注意使用 ScannernextLine 方法的方式:

您遇到的异常是扫描仪未获得任何输入的结果,如可以看出here .我建议您使用 ScannerhasNextLine 方法在扫描仪上重构循环。 .

关于java - NoSuchElementException : No line found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54503199/

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