【问题标题】:Parsing Ints to Strings from Scanner in Try Catch在 Try Catch 中从 Scanner 将整数解析为字符串
【发布时间】:2018-06-04 09:20:59
【问题描述】:

我接到了一项任务,我必须创建一个购物清单程序。我已经在 Python 中完成了这项工作,而且相对简单。但是,在 Java 中我遇到了一些障碍。

这些是我的变量,我知道以这种方式使用静态的问题,最好避免这样做。

private static Scanner input = new Scanner(System.in);
private static String list_add = "string"; //"string" is just a place holder
private static ArrayList listFull = new ArrayList();
private static ArrayList listPos = new ArrayList();
private static int userIn = 1; //1 is also being used as place holder

我用在:

private static void userInput() {
    boolean isValid = false;

    while (!isValid) {
        isValid = true;
        try {
            userIn=Integer.parseInt(input.next());
        }
        catch (InputMismatchException e) {
            System.out.println("That's not a valid number!");
            isValid = false;
        }
    }

}

这样做的原因是需要输入的内容越少,任务完成的速度就越快。在我尝试之前,这是一个很好的哲学。我之前解决这个问题的尝试给出了一个无限循环,我想到的第二个解决方案返回了StackOverflowError。当我问及避免无限循环时,我被引导到另一个问题(Endless loop while using "try and catch " block inside a "while loop"),我认为一开始没有帮助,但发现它是(谢谢标记那个的人)。但是,我没有让这个解决方案起作用,而且我看不出哪里出错了。

在尝试不同的输入以查看它们是否是杀死它的一种特定类型后,我收到了以下错误:

测试用例“strin”:

Exception in thread "main" java.lang.NumberFormatException: For input string: "strin"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at DebuggingMethods.AllIn(DebuggingMethods.java:17)
    at DebuggingMethods.Menu(DebuggingMethods.java:33)
    at DebuggingMethods.main(DebuggingMethods.java:58)

测试用例“十”:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Ten"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at DebuggingMethods.AllIn(DebuggingMethods.java:17)
    at DebuggingMethods.Menu(DebuggingMethods.java:33)
    at DebuggingMethods.main(DebuggingMethods.java:58)

测试用例 int(10):

以预期结果运行。

我以为我缺少了一个模块,所以我做了import java.lang.*; 并没有改变错误。如果有人有解决方案,请帮助我。我找不到已经发布的问题来解释我做错了什么。当我将它从 Try-Catch 中取出时,它正在工作。


整片

import java.util.InputMismatchException;
import java.util.*;
import java.util.Scanner;
import java.lang.*;

public class TestOne {

    private static Scanner input = new Scanner(System.in);
    private static String list_add = "string";
    private static ArrayList listFull = new ArrayList();
    private static ArrayList listPos = new ArrayList();
    private static int userIn = 1;

    private static void userInput() {
        boolean isValid = false;

        while (!isValid) {
            isValid = true;
            try {
                userIn=Integer.parseInt(input.next());
            }
            catch (InputMismatchException e) {
                System.out.println("That's not a valid number!");
                isValid = false;
            }
        }

    }


    public static void main(String[] args) {
        //titleMain();
        userInput(); // For the sake of demonstration


    }

}

【问题讨论】:

  • input.next() 中有什么内容?
  • 应该是String,应该解析成int。不幸的是,这似乎没有发生,所以我真的不知道
  • 你到底想做什么,你能解释一下吗?因为我在程序中找不到任何用户输入,也没有 userin 的声明。
  • menuSelect 指定为 int 变量,用户输入来自menuSelect=Integer.parseInt(input.nextLine());。我要做的就是允许用户输入一个与数字等效的字符串,这是可以做到的。我还试图让我的 try-catch 提示用户在输入无效且无法用作 int 时重试。
  • 请发一个有效的minimal reproducible example

标签: java


【解决方案1】:

你把你的例外搞混了。您正在捕获InputMismatchException,如果输入无效,这就是input.nextInt() 会抛出的内容。但是您实际上是在使用Integer.parseInt 来解析输入,如果输入无效,则会抛出NumberFormatException。由于NumberFormatException 不是InputMismatchException 的子类,因此它不会被捕获,您最终会死于堆栈跟踪。

您实际上根本不需要在此处执行任何异常操作。 Scanner 可以告诉你下一个输入是否是有效整数,然后你可以主动决定如何处理它。请按照以下思路思考:

Scanner sc;
//...
System.out.println("Please enter a number");
while (!sc.hasNextInt()){
    sc.nextLine();  // throw away the bad input
    System.out.println("Please enter a valid number");
}
int theNum = sc.nextInt();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多