- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做一个简单的项目,我从 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将允许您添加输入。您应该注意使用 Scanner
的 nextLine
方法的方式:
您遇到的异常是扫描仪未获得任何输入的结果,如可以看出here .我建议您使用 Scanner 的 hasNextLine
方法在扫描仪上重构循环。 .
关于java - NoSuchElementException : No line found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54503199/
我是一名优秀的程序员,十分优秀!