英文:
Android Studio: java.util.NoSuchElementException: No line found
问题
以下是代码和错误信息的翻译:
我正在尝试在Android Studio中运行以下代码,并且遇到了显示的错误。实际上,每当我使用`scanner.nextLine()`时,都会得到类似的错误。
package com.example.javalib;
import java.util.Scanner;
public class ChallengeThirteen {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入您的姓名:");
String name = scanner.nextLine();
System.out.println("您的姓名是 " + name);
}
}
错误:
> Task :JavaLib:ChallengeThirteen.main() 失败
> 输入您的姓名:
> Exception in thread "main" java.util.NoSuchElementException: 找不到任何行
> at java.util.Scanner.nextLine(Scanner.java:1540)
> at com.example.javalib.ChallengeThirteen.main(ChallengeThirteen.java:12)
>
> 任务“:JavaLib:ChallengeThirteen.main()”执行失败。
> 进程“command '/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java'”以非零退出值1结束
英文:
I am trying to run the following code in Android Studio and getting the shown error. Actually, whenever I use scanner.nextLine()
, I get a similar error.
package com.example.javalib;
import java.util.Scanner;
public class ChallengeThirteen {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Your name is "+ name);
}
}
Error:
> > Task :JavaLib:ChallengeThirteen.main() FAILED
> Enter your name:
> Exception in thread "main" java.util.NoSuchElementException: No line found
> at java.util.Scanner.nextLine(Scanner.java:1540)
> at com.example.javalib.ChallengeThirteen.main(ChallengeThirteen.java:12)
>
> Execution failed for task ':JavaLib:ChallengeThirteen.main()'.
> > Process 'command '/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java'' finished with
> non-zero exit value 1
答案1
得分: 1
你的问题在于你正在使用Android Studio。Android Studio专门设计用于编写Android应用程序,而不是设计用来创建独立的Java应用程序。我建议你使用IntelliJ IDEA代替Android Studio,因为它的界面几乎相同,并且支持独立的应用程序。当你运行程序时,还会得到一个命令行,这将允许你使用System.in输入流,并且会完全消除这个错误。这个错误发生是因为Android Studio默认不允许你从命令行使用System.in输入流,这意味着它不会检测Scanner对象的输入行。
英文:
Your problem is that you're using Android Studio. Android Studio is specifically designed to write applications for Android, and is not designed to be able to create standalone java applications. I would recommend using IntelliJ IDEA instead of Android Studio as its interface is practically identical and it supports standalone applications. You will also get a command line when you run a program, which will let you use the System.in input stream and will get rid of this error entirely. This error occured because Android Studio does not let you use the System.in input stream from the command line by default, which means it will not detect a line for the Scanner object.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论