首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java InputMismatchException catch?

Java InputMismatchException catch?
EN

Stack Overflow用户
提问于 2015-11-25 01:02:59
回答 1查看 75关注 0票数 1

我希望能够处理用户输入int以外的任何内容的异常,这样它将抛出一个可以稍后处理的异常,并向用户显示一条用户友好的消息,建议输入一个Int。

代码语言:javascript
复制
final int FEES=3000;
    String [] user = new String [2];
    String [] password = new String [3];
    int userChoice, sdev, maths, systemAnalysis, cmpArch, menu, studentFees=0, grantFees=0, hundred=0, seventyFive=0, fifty=0, noGrant=0, grantAwarded=0, processed=0, totalStudentFees=0;
    double average;
    String studentName, studentNumber;

    Scanner in=new Scanner(System.in);

    System.out.println("\t\tPlease create your College Grant account");
    System.out.print("Please enter your desired username: ");
    user[0]=in.next();
    System.out.print("\nPlease enter the password for your account: ");
    password[0]=in.next();
    System.out.println("\n\n\t\tSecurity Feature");
    System.out.print("Please enter your username: ");
    user[1]=in.next().toLowerCase();
    System.out.print("\nPlease enter your password: ");
    password[1]=in.next();
    System.out.print("\nPlease re-enter your password: ");
    password[2]=in.next();

    if(user[1].equals(user[0]) && password[1].equals(password[0]) && password[2].equals(password[0])){
        System.out.println("\nPassword has been verified successfully.");
        System.out.println("\n\n\t\t College Grant System");
        System.out.println("1.\t Calculate Grant");
        System.out.println("2.\t Fee Statistics");
        System.out.println("3.\t Grant Category Information");
        System.out.println("4.\t Exit");
        System.out.print("\nEnter your choice: ");
        userChoice=in.nextInt();
        while (userChoice!=1 && userChoice!=2 && userChoice!=3 && userChoice!=4){
            System.out.println("\t Inavlid Choice");
            System.out.println("\n\t\t College Grant System");
            System.out.println("1.\t Calculate Grant");
            System.out.println("2.\t Fee Statistics");
            System.out.println("3.\t Grant Category Information");
            System.out.println("4.\t Exit");
            System.out.print("\nEnter your choice: ");
            userChoice = in.nextInt();
        }
EN

回答 1

Stack Overflow用户

发布于 2015-11-25 01:20:01

下面是一个输入循环的快速示例,用于检查输入是否不是数字,或者是否超出范围:

代码语言:javascript
复制
import java.util.Scanner;

public class InputLoop {

    Scanner scan;

    public InputLoop()
    {
        //Initialize number outside of the range, so the while loop condition will be true on its first run.
        int numberToEnter = -1;

        scan = new Scanner(System.in);

        while (numberToEnter < 1 || numberToEnter > 4)
        {
            System.out.print("Enter a number 1 through 4: ");
            try {
                numberToEnter = Integer.parseInt(scan.nextLine());
            } catch (NumberFormatException ex)
            {
                System.out.println("Not a number!");
            }
        }
        System.out.print("Thank you! You entered: " + numberToEnter);
    }
    public static void main(String...args)
    {
        new InputLoop();
    }
}

输出示例:

代码语言:javascript
复制
Enter a number 1 through 4: 12
Enter a number 1 through 4: 16
Enter a number 1 through 4: banana
Not a number!
Enter a number 1 through 4: 2
Thank you! You entered: 2
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33899626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档