Java 如果数字系统输入无效,则显示错误

Java 如果数字系统输入无效,则显示错误,java,binary,hex,octal,Java,Binary,Hex,Octal,我想用Java编写数字系统到ASCII转换器的代码(我已经试过了) 但是如果输入错误,我想输出“无效”,而不是退出程序并显示错误。希望有可能 我已经得到了转换后的输出。我只想输入一个“无效”的输出,以防我选择的数字系统和输入不匹配 do{ opt=displayMainMenu(); switch(opt){ case 1: binary=getBinary();

我想用Java编写数字系统到ASCII转换器的代码(我已经试过了) 但是如果输入错误,我想输出“无效”,而不是退出程序并显示错误。希望有可能

我已经得到了转换后的输出。我只想输入一个“无效”的输出,以防我选择的数字系统和输入不匹配

  do{
        opt=displayMainMenu();

        switch(opt){
            case 1: 
                    binary=getBinary();
                    System.out.println("\n\tASCII Character: "+ (char)binary);
                    in.readLine();
                    break;

            case 2: 
                    octal=getOctal();
                    System.out.println("\n\tASCII Character: "+(char)octal);
                    in.readLine();
                    break;

            case 3:
                    decimal=getDecimal();
                    System.out.println("\n\tASCII Character: "+(char)decimal);
                    in.readLine();
                    break;
            //case 4: still don't have Hexadecimal since I find it difficult. Sorry  
            case 5: 
                    System.out.println("\f\n\tGood Bye!");
                    break;

            default: 
                    System.out.print("\n\tInvalid Option.");
                    in.readLine();   
        }


    }while(opt!=5);
//我就发一张

          public static int getOctal() throws Exception{
          BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        Scanner s=new Scanner(System.in);   
    System.out.println("\f\n\t\t- Octal -\n");

    System.out.print("\n\tInput Octal Number:  ");
    String n=s.nextLine();

    int oct = Integer.parseInt(n,8);
    return oct;
}
样本输入:

选择数字系统:2//八进制到ASCII

输入八进制数:3A//为十六进制

输出:无效

选择数字系统:2//八进制到ASCII

输入八进制数:041//哪个是正确的


输出:ASCII是

如果你问我,我认为你的getOctal()方法应该被称为getIntegerFromOctal()。但嘿…也许那只是我

许多人所做的是将解析包装在一个try/catch块中,并捕获任何错误,类似这样:

public static int getIntegerFromOctal() {
    Scanner s = new Scanner(System.in);
    String ls = System.lineSeparator();

    System.out.println(ls + "    - Octal -");
    while (true) {
        System.out.print("Input Octal Number:  ");
        String n = s.nextLine();
        int oct = 0;
        try {
            oct = Integer.parseInt(n, 8);
            return oct;
        }
        catch (NumberFormatException ex) {
            System.err.print("Invalid Octal Value Supplied! Try again..." + ls + ls);
        }
    }
}
另一种方法是将(RegEx)与以下方法结合使用:


为什么不将代码块包装在“while(true)”语句中?打印无效后,它将再次作为输入并继续。是的,我使用的是while循环语句。但我想知道是否可以检查数字系统输入是否对我选择的案例有效。我是说。如果我选择案例2(八进制),然后输入“0100010”(但它甚至不是一个八进制数),该怎么办。抱歉弄得这么乱:/
public static int getIntegerFromOctal() {
    Scanner s = new Scanner(System.in);
    String ls = System.lineSeparator();

    System.out.println(ls + "    - Octal -");
    while (true) {
        System.out.print("Input Octal Number:  ");
        String n = s.nextLine();
        int oct = 0;
        /* The String#matches() method is used here with a
           regex that ensures that only digits from 0 to 7 
           (octal digits) are supplied. If you want to also
           ensure Integer Literal where the integer value
           supplied starts with a 0 then use: "^0[0-7]+$" */
        if (!n.matches("^[0-7]+$")) {
            System.err.print("Invalid Octal Value (" + n + ") Supplied! Try again..." + ls + ls);
            continue;
        }
        oct = Integer.parseInt(n, 8);
        return oct;
    }     
}