英文:
JAVA Scanner NoSuchElementException: No line found
问题
抱歉,我只会返回翻译后的代码部分,不包括其他内容。以下是您提供的代码的翻译:
抱歉,这是一个初学者的问题。您能帮我解决与 Scanner 相关的问题吗?
[背景]:
我编写了一些代码来学习 Scanner:
import javax.swing.JOptionPane;
import java.util.Scanner;
class Main {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("今天是星期几?");
String day = scanner.nextLine();
System.out.print("今天是几月?");
String month = scanner.nextLine();
String outputText = "今天" + day + "号,月份 - " + month;
JOptionPane.showMessageDialog(null, outputText);
}
}
我遇到了一个异常:找不到行。
这是我在输出中得到的内容:
任务 :run 失败 今天是星期几? 主线程中的异常
java.util.NoSuchElementException: 找不到行,位于
java.base/java.util.Scanner.nextLine(Scanner.java:1651) 位于
DemoOne.Main.main(Main.java:10)失败: 构建过程异常终止。
我正在使用 Apache NetBeans IDE 12.1。
看起来与 System.in 有关的问题,但我不知道如何修复它。
请提供支持。
英文:
Sorry for baby question but I am very beginner.
Please can you support with concern related to Scanner.
[BACKGROUND]:
I wrote some code to study Scanner:
import javax.swing.JOptionPane;
import java.util.Scanner;
class Main {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("What is day today? ");
String day = scanner.nextLine();
System.out.print("What is mounth today?");
String mounth = scanner.nextLine();
String outputText = "Today" + day + "mounth - " + mounth;
JOptionPane.showMessageDialog(null, outputText);
}
}
I have got an Exception: No line found.
That is what I get in the output:
> Task :run FAILED What is day today? Exception in thread "main"
> java.util.NoSuchElementException: No line found at
> java.base/java.util.Scanner.nextLine(Scanner.java:1651) at
> DemoOne.Main.main(Main.java:10)
>
> FAILURE: Build failed with an exception.
I am using Apache NetBeans IDE 12.1.
Looks like there is some of concern with System.in but I cannot understand how to fix it.
Please support.
答案1
得分: 1
你只需要确保你使用了 System.out.println()
当你询问是哪个月份时,你只使用了 System.out.print()
见下方代码:
Scanner scanner = new Scanner(System.in);
System.out.println("今天是几号?");
String day = scanner.nextLine();
System.out.println("今天是哪个月?"); // 就在这里
String mounth = scanner.nextLine();
String outputText = "今天" + day + "号,月份 - " + mounth;
JOptionPane.showMessageDialog(null, outputText);
英文:
You just need to make sure you are using System.out.println()
When you ask what month it is, you only used System.out.print()
See below
Scanner scanner = new Scanner(System.in);
System.out.println("What is day today? ");
String day = scanner.nextLine();
System.out.println("What is mounth today?"); //right here
String mounth = scanner.nextLine();
String outputText = "Today" + day + "mounth - " + mounth;
JOptionPane.showMessageDialog(null, outputText);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论