【问题标题】:using string to break out of loop in java在java中使用字符串跳出循环
【发布时间】:2015-08-16 21:38:02
【问题描述】:

如果用户在扫描仪中输入“STOP”,我只想跳出while true 循环。目前我的扫描仪只接受整数,我相信这就是问题所在。如果我尝试输入“STOP”,我会收到很多错误,说“线程主线程中的异常”。这是我的代码的 sn-p:

public class algorithm {
private Scanner input = new Scanner(System.in);
int n;

while (true){
    System.out.println("Eneter a number to check if it is odd or even, then hit enter: ");
    System.out.println("Type 'STOP' to end the program");
    n = input.nextInt();

    String strN = String.valueOf(n); //convert integer to string
    if (new String(strN).equals("STOP")){ //if string is STOP, breaks
        System.out.println("Thanks for using my program");
        break;
    }

    if (n % 2 == 0){
        System.out.println(n+" is even");
        continue;
    }
    else{
        System.out.println(n+" is odd");
        continue;

我知道我缺少一些右花括号,但请放心,它们都在我的实际代码中。谢谢。

这是我得到的错误: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at OddOrEven.algorithm.checker(algorithm.java:13) at OddOrEven.main.main(main.java:7)

【问题讨论】:

  • .nextInt()?您需要提示输入字符串,而不是 int。
  • 我认为nextInt() 不会检查用户是否输入“停止”。
  • 是的,我已经试过了。但是我不必将字符串转换为整数来检查它们是奇数还是偶数?
  • 异常类型是什么?
  • 提示输入字符串,然后检查该字符串是否为“STOP”,否则尝试将其转换为 int。

标签: java string integer


【解决方案1】:

您自己已经发现了问题 - 您的 Scanner 只读取整数:

int n;
...
n = input.nextInt();

所以变量 n(一个 int)不可能包含字符串“STOP”(当您调用 nextInt() 时,您的 Scanner 无论如何都会抛出异常,但它会遇到一个字符串,例如“STOP”,即它不能转换为 int)。

为此,您需要从输入中读取字符串(可能使用Scanner.nextLine()),检查它们是否为“STOP”,如果不是,则仅尝试使用以下方式将它们转换为整数:

int n = Integer.parseInt(mystring)

要处理垃圾输入(既不是 STOP 也不是整数),请将 parseInt 行包装在 try-catch 块中,以便您可以通过捕获 Exception 来检测输入何时为垃圾

try {
  int i = Integer.parseInt(mystring);
  // do something with the int
}
catch (NumberFormatException e) {
  // display warning message to the user instead
}

另见related question

【讨论】:

  • 感谢您的回复,非常有帮助!
  • 如何检查用户是否输入了诸如“dsfasdfsa”之类的垃圾字符串,然后打印出“请输入有效输入”之类的内容?因为目前,我正在做你所做的事情,如果用户没有输入 STOP,我将其转换为字符串。但是如果我输入一个垃圾字符串,我会得到很多错误。
  • parseInt 行包装在try-catch 块中,以便您可以通过捕获Exception 来检测输入何时为垃圾
  • 嘿,我实际上创建了一个包含 try-catch 块的单独方法,效果很好。非常感谢!
【解决方案2】:

最简单的方法可能是使用 input.next() 而不是 input.nextInt()。使用 input.next() 会将输入作为字符串读取,然后您可以检查输入是否等于“QUIT”,如果不是,您可以使用 Integer.parseInt 从读取的字符串中解析 Integer

【讨论】:

  • 如果您觉得有帮助,请采纳答案:)
【解决方案3】:

如下所示,应该可以解决。

注意:没有测试过编译错误,只是写出来(但你得到了一个要点)

     public Scanner input = new Scanner(System.in);
    int n;

while (true){
    System.out.println("Eneter a number to check if it is odd or even, then hit enter: ");
    System.out.println("Type 'STOP' to end the program");
    n = input.next();
    Integer input;

   // String strN = String.valueOf(n); //convert integer to string
    if (strN.equals("STOP")){ //if string is STOP, breaks
        System.out.println("Thanks for using my program");
        break;
    }
    else{
        try{
    input=  Integer.parseInt(strN);

        }
        catch(Exception e)
        {
            System.out.println("Please enter a  number");
        }
    }

    if (input % 2 == 0){
        System.out.println(n+" is even");
        continue;
    }
    else{
        System.out.println(n+" is odd");
        continue;
    }

【讨论】:

    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2015-08-20
    • 2021-12-10
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多