- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个等待数字 (int) 的非常简单的循环,只要该数字不是 exitOption
它就不会离开循环,但是我得到了一个意外错误,我没有不知道是什么原因造成的。
编辑
添加另一个片段以便您可以编译
public static void main(String[] args) throws FileNotFoundException,
SecurityException,
IOException,
ClassNotFoundException {
while (controller.selectOptionMM());
/编辑
public boolean selectOptionMM() throws SecurityException,
FileNotFoundException,
IOException {
int cmd = ui.getExitOption();
ui.mainMenu();
cmd = utils.readInteger(">>> "); // this is my problem, right here
// code in next snippet
while (cmd <1 || cmd > ui.getExitOption()) {
System.out.println("Invalid command!");
cmd = utils.readInteger(">>> ");
}
switch (cmd) {
case 1:
case 2:
case 3:
case 4: this.repository.close();
return true;
case 5: return false;
}
return false;
}
这里是失败的地方:
public int readInteger(String cmdPrompt) {
int cmd = 0;
Scanner input = new Scanner(System.in);
System.out.printf(cmdPrompt);
try {
if (input.hasNextInt())
cmd = input.nextInt(); // first time it works
// Second time it does not allow me to input anything
// catches InputMissmatchException, does not print message
// for said catch
// infinitely prints "Invalid command" from previous snippet
} catch (InputMismatchException ime) {
System.out.println("InputMismatchException: " + ime);
} catch (NoSuchElementException nsee) {
System.out.println("NoSuchElementException: " + nsee);
} catch (IllegalStateException ise) {
} finally {
input.close(); // not sure if I should test with if (input != null) THEN close
}
return cmd;
}
第一次 我通过了槽,它读取数字没问题。现在,如果数字不是 5(在本例中为 exitOption),它会再次通过 readInteger(String cmdPrompt)
,除了这次它会跳转到 catch (InputMismatchException ime)
(调试) 除了它不打印该消息而只是跳转到 Error, input must be number
和 Invalid command
。
我的输入缓冲区中是否有东西卡住了,我可以刷新它吗,为什么它(输入缓冲区)卡住了(带有随机数据)???
我会再次尝试调试,看看我的输入缓冲区中卡住了什么,如果我能弄清楚如何查看的话。
最佳答案
问题出在对 input.close()
的调用中 - 这会导致关闭基础输入流。当被关闭的输入流是 System.in
时,就会发生不好的事情(即,您无法再从标准输入读取数据)。删除这一行应该没问题。
关于java - 来自扫描仪的 hasNextInt() 行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14060165/
读完 hasNextInt() API 后,我仍然对这段代码感到困惑。 Scanner input = new Scanner(System.in); while (input.hasNextInt(
我有一个格式如下的 .txt 文件 The Shawshank Redemption 100 19.95 DVD 还有很多行,但我正在尝试将这些值存储到四个不同的 ArrayList 中。问题是出现错
这个问题已经有答案了: Check if String contains only letters (17 个回答) 已关闭 7 年前。 我是java新手,我想知道是否有其他方法可以代替has.nex
我有一个文件,每一行都有不同的整数。例如: 5 4 3 2 1 我正在尝试编写一个程序来遍历每个 int,并将该 int 放入一个数组中。到目前为止,我的代码是: Scanner sc =
我是初学者,我对 hasNextInt() 的使用感到困惑。如果它检查输入,那么我们不应该在请求用户输入后使用它吗?但是,在下面给定的代码中,它与 if 语句一起使用。请指教。 import java
这段代码没问题并且运行良好。 public static void main(String[] args) { String [] keywords={"auto","break","case
我知道这听起来像是一个非常愚蠢的问题,但我不明白我的错误在哪里。 为什么在循环的第二次迭代中,它不打印'Enter a number:'? import java.util.Scanner; publ
我正在尝试使用scanner.hasNextInt()方法从用户那里获取一些整数输入。它工作得很好,但我需要输入一个字母让程序知道我已完成输入整数,而我希望控制台知道在我在一行后按回车键后。 例如,我
我正在尝试 build 一个与用户指示的高度相同的金字塔。如果我使用下面的代码,它会告诉我我没有引入整数 btw 1-20...只要我不插入数字 0 && rows0 && rows 尝试这样做: i
我正在尝试创建一个程序,提示用户输入输入并仅在输入为整数时才显示输出 如果用户输入非整数值,程序将循环直到得到一个整数值 如果输入的是整数,程序将显示其值 我想要的输出将是这样的: 输入一个数字:a
嗨,我真的很困惑!从下面的代码中,您可以看到 obj.NextInt() 要求输入,并且还要求输入obj.hasNextInt() 需要输入内容。 Scanner obj=new Scanner(Sy
我想做的是让用户输入很多数字,然后按回车键,然后将所有这些数字一次存储到堆栈中。我的想法是使用循环来遍历所有数字并将它们压入堆栈,如下所示: Stack mainBin = new Stack();
你好,我的问题是这个程序。 int integer = 0; int evenInts = 0; Scanner in = new Scanner(System.in); System.out.pri
这个问题在这里已经有了答案: How to use java.util.Scanner to correctly read user input from System.in and act on
这个问题在这里已经有了答案: Validating input using java.util.Scanner [duplicate] (6 个答案) 关闭 5 年前。 public static
我有一个等待数字 (int) 的非常简单的循环,只要该数字不是 exitOption 它就不会离开循环,但是我得到了一个意外错误,我没有不知道是什么原因造成的。 编辑 添加另一个片段以便您可以编译 p
我不知道这里有什么问题 import java.util.Scanner; public class CountingInts { public static void main(String
我正在编写一个简单的程序,它从 Scanner 对象接收整数输入,确定它是否是回文,并返回 boolean 值。 对于大多数数字,它运行良好。但是,在这段代码中: private static voi
我正在使用以下代码: while (invalidInput) { // ask the user to specify a number to update the times by
该程序要求用户输入学号,学号必须是 0-999999 之间的整数。已被注释掉的 testID 方法使用 do while 循环和内部 while 循环,确保用户仅输入整数值。此方法运行没有任何问题。每
我是一名优秀的程序员,十分优秀!