java.util.InputMismatchException扫描器问题?

huangapple go评论121阅读模式
英文:

java.util.InputMismatchException scanner issue?

问题

我正在尝试通过键盘扫描器收集用户输入,并验证输入是否与存储的值匹配。因此,如果输入是 2927942074l,应该显示行 Correct,但实际上我收到了以下错误。此外,如果输入与 PIN 不匹配,则会显示行 Wrong。不确定我做错了什么。

  1. import java.util.Scanner;
  2. class app {
  3. public static void main(String[] args)
  4. {
  5. long pin = 2927942074l;
  6. System.out.println("Please enter your pin.");
  7. Scanner keyboard = new Scanner(System.in);
  8. long input = keyboard.nextLong();
  9. if (input != pin)
  10. System.out.println("Wrong");
  11. if (input == pin)
  12. System.out.println("Correct");
  13. }
  14. }
英文:

I am trying to collect user input via the keyboard scanner and verifying if the input matches the stored value. So if the input is 2927942074l the line Correct should appear however I get the following error. Also if the input doesn't match with the pin the line Wrong appears. Not sure what I am doing wrong.

  1. Exception in thread "main" java.util.InputMismatchException
  2. at java.base/java.util.Scanner.throwFor(Scanner.java:939)
  3. at java.base/java.util.Scanner.next(Scanner.java:1594)
  4. at java.base/java.util.Scanner.nextLong(Scanner.java:2373)
  5. at java.base/java.util.Scanner.nextLong(Scanner.java:2328)
  6. at app.main(app.java:11)
  1. import java.util.Scanner;
  2. class app {
  3. public static void main(String[] args)
  4. {
  5. long pin = 2927942074l;
  6. System.out.println("Please enter your pin.");
  7. Scanner keyboard = new Scanner(System.in);
  8. long input = keyboard.nextLong();
  9. if (input != pin)
  10. System.out.println("Wrong");
  11. if (input == pin)
  12. System.out.println("Correct");
  13. }
  14. }

答案1

得分: 0

你的代码是正确的,并且按照要求工作。

你需要输入的是 2927942074,而不是 2927942074l

英文:

Your code is correct and works as per requirement.

The thing that you need to input is 2927942074 not 2927942074l

答案2

得分: 0

  1. Type just the number `2927942074` without trailing 'l'. Else it will treat it as a String.
  2. Also you may want to add a condition using keyboard.hasNextLong() if you dont want to take String inputs.
  3. if (keyboard.hasNextLong()) {
  4. long input = keyboard.nextLong();
  5. if (input != pin)
  6. System.out.println("Wrong");
  7. if (input == pin)
  8. System.out.println("Correct");
  9. } else {
  10. System.out.println("Enter a valid pin");
  11. }
英文:

Type just the number 2927942074 without trailing 'l'. Else it will treat it as a String.
Also you may want to add a condition using keyboard.hasNextLong() if you dont want to take String inputs.

  1. if (keyboard.hasNextLong()) {
  2. long input = keyboard.nextLong();
  3. if (input != pin)
  4. System.out.println("Wrong");
  5. if (input == pin)
  6. System.out.println("Correct");
  7. } else {
  8. System.out.println("Enter a valid pin");
  9. }

答案3

得分: 0

你必须输入

2927942074

而不是

2927942074l

“L” 只是为了让 Java 编译器理解这些数字表示的是长整型而不是整型(整型是默认的)。

英文:

You have to input

  1. 2927942074

not

  1. 2927942074l

The "L" is only for the Java Compiler to understand, that die digits resemble a long and not an int (which would be default).

huangapple
  • 本文由 发表于 2020年8月28日 14:37:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63628694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定