1

我想以更具体的方式处理 NumberFormatException 。当它尝试分配除整数以外的任何内容时,当输入以下内容时,会发生此异常:

  1. 细绳
  2. 特点
  3. 空输入
  4. 双数

根据输入的内容,我想显示正确的消息,例如

您已输入字符串,请输入整数

或者

值不能为空,请输入整数值

下面的代码NumberFormatException以一般方式捕获。

我想知道有没有办法包含更多的 catch 子句。

    import java.util.Scanner;

    public class TestException {

        static int input;
        static Scanner scan = new Scanner(System.in);

        public static void main(String[] args) {
            System.out.println("Enter an integer number: ");

            try {
                input = Integer.parseInt(scan.next());
                System.out.println("You've entered number: " + input);
            } catch (NumberFormatException e) {
                System.out.println("You've entered non-integer number");
                System.out.println("This caused " + e);
            }
        }
    }
4

5 回答 5

5

首先从用户那里获取输入,然后尝试将其转换为整数。

    static int input;
    static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("Enter an integer number: ");

        String inputString = scan.next();

        try {
            input = Integer.parseInt();
            System.out.println("You've entered number: " + input);
        } catch (NumberFormatException e) {
            if(inputString.equals("") || inputString == null) {
                System.out.println("empty input"); 
            } else if(inputString.length == 1) {
                System.out.println("char input");
            } else {
                System.out.println("string input");
            }
        }
    }
于 2013-01-10T07:07:10.020 回答
2

您必须使用if-else构造在catch块内指定您的场景。

请看下面的代码:

String inString = null;
try 
{
    iString = scan.next().trim();
    input = Integer.parseInt(inString);
    System.out.println("You've entered number: " + input);
} 
catch (NumberFormatException e) 
{
    if(inString.equals("")
    {
        System.out.println("You've entered empty string.");
    }
    else if(inString.length() == 1)
    {
        System.out.println("You've entered a single char");
    }
    else
    {
        System.out.println("You've entered non-intereger number");
    }
    System.out.println("This caused " + e);
}
于 2013-01-10T07:06:58.023 回答
1

如果将输入解析为整数值导致异常,您可以对输入进行更多测试,如下所示:

String scanned = null
try {
   scanned = scan.next();
   input = Integer.parseInt(scanned);
   System.out.println("You've entered number: " + input);
} catch (NumberFormatException e) {
   if (scanned == null || scanned.isEmpty()) {
      System.out.println("You didn't enter any value");
   } else if (scanned.length() == 1)
      System.out.println("You entered a single char which is not a number");
   }
   // and more tests, you can even try to parse as Double
}
于 2013-01-10T07:08:10.360 回答
0
String aString = null;
    aString = scan.next().trim();   
    System.out.println("You've entered number: " + aString);
    if("".equals(aString.trim())){
       System.out.println("You have entered an Empty String");
     }else if(!isNumber(aString) && aString.length()==1){
       System.out.println("You have entered a Character");
     }else if(!isNumber(aString) && aString.length()>1){
       System.out.println("You have entered a String");
     }else if(isNumber(aString)){
       int input = Integer.parseInt(aString.replaceAll(",",""));
       System.out.println("You have entered a correct Number"+input); 
     }

   private boolean isNumber(String s){
        return s.matches("[0-9]+(,[0-9]+)*,?");
    }
于 2013-01-10T07:18:29.547 回答
0
public static int isInt(){
        boolean ok=false;
        int b=1;
        do{
            String next = sc.next();
            int a=b;
            try{
             a = Integer.parseInt(next);        
            }catch(Exception e){

                System.out.println("Invalid Option...");
                continue;
            }


            if(a==1){b=a;ok=true;}
            else if(a==2){b=a;ok=true;}
            else if(a==3){b=a;ok=true;}

        }while(!ok);
    return b;
}
于 2018-03-17T08:27:53.393 回答