- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以,我被这段代码困住了:
import java.util.InputMismatchException;
import java.util.Scanner;
public class ConsoleReader {
Scanner reader;
public ConsoleReader() {
reader = new Scanner(System.in);
//reader.useDelimiter(System.getProperty("line.separator"));
}
public int readInt(String msg) {
int num = 0;
boolean loop = true;
while (loop) {
try {
System.out.println(msg);
num = reader.nextInt();
loop = false;
} catch (InputMismatchException e) {
System.out.println("Invalid value!");
}
}
return num;
}
}
这是我的输出:
Insert a integer number:
Invalid value!
Insert a integer number:
Invalid value!
...
最佳答案
根据javadoc对于扫描仪:
When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.
这意味着如果下一个标记不是 int
,它会抛出 InputMismatchException
,但标记仍保留在那里。因此,在循环的下一次迭代中,reader.nextInt() 再次读取相同的标记并再次引发异常。你需要的是用完它。在 catch
中添加一个 reader.next()
来消耗 token ,该 token 无效,需要丢弃。
...
} catch (InputMismatchException e) {
System.out.println("Invalid value!");
reader.next(); // this consumes the invalid token
}
关于java - 如何使用 Scanner 处理无效输入(InputMismatchException)引起的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56876279/
这个问题已经有答案了: Scanner double value - InputMismatchException (2 个回答) 已关闭2 年前。 为什么 Java 在使用 Scanner.next
我正在实现一个驱动程序来测试决策树,但遇到输入不匹配异常。我可以做什么来解决这个问题? 这是我的代码: import java.io.FileNotFoundException; /** * Loa
static long arrayMaxMin[]; public static void main(String[] args) { try{ Scanner in = new
这个问题在这里已经有了答案: Scanner is skipping nextLine() after using next() or nextFoo()? (23 个回答) 5年前关闭。 impor
简介 我在捕获 InputMismatchException 时遇到问题。当我搜索 InputMismatchException 时,我已经阅读了该网站上出现在第一页上的所有线程。我在阅读这些帖子时找
while(inputbook.hasNext()){ id = inputbook.nextInt(); name = inputbook.next(); year = in
我有这个代码: import java.util.Scanner; public class Maggiore3Valori { public static void main(String[
public static void main(String[] args) { try { Scanner scan = new Scanner(new Fi
当输入 55.5 作为第一个提示的答案时,我收到以下错误。输入 50 时我没有收到错误消息。 Exception in thread "main" java.util.InputMismatchExc
为什么我遇到了 inputMismatchException?当输入仅为整数时,结果会很好,但是当我使用双输入时,编译器会抛出 inputMismatchException。 public stati
我有一个任务: 5-8)(求最高分)编写一个程序,提示用户输入学生人数以及每个学生的姓名和分数,最后显示分数最高的学生的名字。 当我运行此代码并输入“john”等名称时,它运行良好,但当我尝试输入“J
我尝试以最简单的方式使用扫描仪: 代码: double gas, efficiency, distance, cost; Scanner scanner = new Scanner(System.in
我试图提示用户输入 x 坐标,但是当我输入十进制数(例如 2.1)作为输入时,这会导致问题。如何解决该问题? import java.util.Scanner; public cl
所以我一直在做一项任务,要求我们建立一个“患者经理”,根据紧急级别确定首先治疗的患者。我遇到的主要问题是,当用户在紧急栏中输入数字以外的内容时,它会使程序崩溃并给我一个 InputMismatchEx
public static void main(String[] args) throws FileNotFoundException { File file = new File("data
我写了下面这段Java代码。当运行它并输入任何值(定义的值,例如 latte,或任何其他值,例如 az 整数)时,我得到一个 InputMismatchException。 据我所知,此异常意味着输入
我希望用户输入一行文本,只要 x 不等于 numOfContestans 的值。当我运行代码时,我收到一个 InputMismatchException。有人知道如何解决这个错误吗? try {
这个问题已经有答案了: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个回答) 已关闭 7 年前。 我有文本文
我无法找到错误的根源。我所做的就是从文件中读取文本 public static void main(String[] args) throws Exception { int T; S
我在将非整数输入整数字段时遇到问题。我只是采取预防措施,这样如果其他人使用/处理我的程序,他们就不会得到此 InputMismatchException。 当我在 input 变量中输入非数字字符时,
我是一名优秀的程序员,十分优秀!