
在使用java的java.util.scanner类获取用户整数输入时,通常会调用nextint()方法。然而,如果用户输入了非数字字符(例如字母、符号或包含空格的字符串),nextint()方法将无法成功解析,并立即抛出inputmismatchexception运行时异常,导致程序意外终止。
在初始的程序设计中,开发者可能尝试通过!sc.hasNextInt()来判断输入是否为整数,但此判断通常位于int choice = sc.nextInt();之后。这意味着,一旦nextInt()方法被调用且遇到非整数输入,异常就已经抛出,程序崩溃,!sc.hasNextInt()这行代码根本不会被执行到。
以下是导致程序崩溃的典型代码片段:
import java.util.Scanner;
public class Small_Programming_Assignment {
    public static void getSelection() {
        Scanner sc = new Scanner(System.in);
        // ... 其他输出语句 ...
        System.out.println("Enter your selection: ");
        int choice = sc.nextInt(); // 如果此处输入非整数,程序会立即崩溃,抛出InputMismatchException
        // 以下判断语句将不会被执行到
        if (!sc.hasNextInt() ) { 
            System.out.println("Invalid option. Try again.");
            getSelection();
        } 
        // ... 其他逻辑 ...
    }
    // ... 其他方法 ...
}当用户输入非整数值时,如“abc”,程序会在执行sc.nextInt()时直接抛出InputMismatchException,导致程序终止,用户无法得到友好的错误提示。
InputMismatchException是java.util.InputMismatchException类的简称,它是java.util.Scanner类在尝试将输入标记(token)转换为预期类型失败时抛出的运行时异常。根据Oracle官方文档,当检索到的标记与预期类型的模式不匹配,或者标记超出预期类型的范围时,就会抛出此异常。这意味着,Scanner.nextInt()严格要求输入必须是符合整数格式的字符串。
立即学习“Java免费学习笔记(深入)”;
为了有效应对InputMismatchException,避免程序崩溃,并提供友好的用户体验,我们应该采用Java的异常处理机制——try-catch语句块。try块用于包含可能抛出异常的代码,而catch块则用于捕获并处理特定类型的异常。
以下是使用try-catch改进用户选择输入逻辑的示例:
import java.util.InputMismatchException; // 导入InputMismatchException
import java.util.Scanner;
public class RobustInputExample {
    public static void main(String[] args) {
        getSelection();
        // 实际应用中可以根据需要调用其他方法
    }
    public static void getSelection() {
        Scanner sc = new Scanner(System.in); // 每次调用都创建新的Scanner实例
        int choice = 0; // 初始化选择变量
        System.out.println("\nWelcome to the Word Games program menu.");
        System.out.println("Select from one of the following options.");
        System.out.println("1. Substring problem.");
        System.out.println("2. Points problem.");
        System.out.println("3. Exit.");
        System.out.print("Enter your selection: "); // 使用print而非println,让输入紧跟提示
        try {
            choice = sc.nextInt(); // 尝试获取整数输入
        } catch (InputMismatchException e) { // 捕获InputMismatchException
            System.out.println("Invalid input. Please enter a valid number (1, 2, or 3).");
            sc.next(); // 关键步骤:清除Scanner缓冲区中的无效输入,避免下一次读取时再次遇到相同错误
            getSelection(); // 递归调用,重新获取输入
            return; // 捕获异常后,结束当前方法的执行,避免后续逻辑错误
        } finally {
            // 对于System.in的Scanner,通常不在此处关闭,因为它是一个共享资源。
            // 如果是文件或其他流的Scanner,应在此处关闭以释放资源。
        }
        // 处理有效整数输入
        if (choice == 1) {
            System.out.println("Entering Substring Problem...");
            // substringProblem(); // 实际调用对应的游戏方法
            getSelection(); // 游戏结束后返回菜单
        } else if (choice == 2) {
            System.out.println("Entering Points Problem...");
            // pointsProblem(); // 实际调用对应的游戏方法
            getSelection(); // 游戏结束后返回菜单
        } else if (choice == 3) {
            System.out.println("Goodbye!");
            System.exit(0); // 退出程序
        } else {
            System.out.println("Invalid option. Please enter 1, 2, or 3.");
            getSelection(); // 输入范围外,重新获取输入
        }
        // sc.close(); // 通常不关闭System.in的Scanner,除非程序即将退出
    }
    // 示例方法,实际应用中会包含业务逻辑
    public static void substringProblem() {
        System.out.println("Substring Problem Logic Here.");
    }
    public static void pointsProblem() {
        System.out.println("Points Problem Logic Here.");
    }
}在上述代码中:
避免递归过深: 虽然递归调用在示例中有效,但如果用户连续输入大量无效数据,可能会导致StackOverflowError。对于用户输入循环,更推荐使用while循环结构来替代递归,以提高程序的健壮性和可维护性:
import java.util.InputMismatchException;
import java.util.Scanner;
public class RobustInputLoopExample {
    public static void main(String[] args) {
        getSelectionLoop();
    }
    public static void getSelectionLoop() {
        Scanner sc = new Scanner(System.in); // 整个应用程序生命周期中通常只需一个Scanner实例
        int choice = 0;
        boolean validInput = false;
        while (!validInput) {
            System.out.println("\nWelcome to the Word Games program menu.");
            System.out.println("Select from one of the following options.");
            System.out.println("1. Substring problem.");
            System.out.println("2. Points problem.");
            System.out.println("3. Exit.");
            System.out.print("Enter your selection: ");
            try {
                choice = sc.nextInt();
                if (choice >= 1 && choice <= 3) {
                    validInput = true; // 输入有效且在范围内,跳出循环
                } else {
                    System.out.println("Invalid option. Please enter 1, 2, or 3.");
                    // 继续循环,重新提示输入
                }
            } catch (InputMismatchException e) {
                System.out.println("Invalid input. Please enter a number (1, 2, or 3).");
                sc.next(); // 清除无效输入
                // 继续循环,重新提示输入以上就是构建健壮的Java用户输入:Scanner整数解析与异常捕获的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号