【问题标题】:How to check the input is an integer or not in Java? [duplicate]如何在 Java 中检查输入是否为整数? [复制]
【发布时间】:2013-11-24 08:35:48
【问题描述】:

在我的程序中,我希望用户输入一个整数。我希望在用户输入不是整数的值时显示错误消息。 我怎样才能做到这一点。 我的程序是找到圆的面积。用户将在其中输入半径值。但是,如果用户输入一个字符,我希望显示一条消息说输入无效。

这是我的代码:

int radius, area;
Scanner input=new Scanner(System.in);
System.out.println("Enter the radius:\t");
radius=input.nextInt();
area=3.14*radius*radius;
System.out.println("Area of circle:\t"+area);

【问题讨论】:

    标签: java


    【解决方案1】:

    如果您使用Scanner 获取用户输入,您可以这样做:

    if(yourScanner.hasNextInt()) {
        yourNumber = yourScanner.nextInt();
    }
    

    如果不是,则必须将其转换为 int 并捕获 NumberFormatException

    try{
        yourNumber = Integer.parseInt(yourInput);
    }catch (NumberFormatException ex) {
        //handle exception here
    }
    

    【讨论】:

      【解决方案2】:

      你可以试试这个方法

       String input = "";
       try {
         int x = Integer.parseInt(input); 
         // You can use this method to convert String to int, But if input 
         //is not an int  value then this will throws NumberFormatException. 
         System.out.println("Valid input");
       }catch(NumberFormatException e) {
         System.out.println("input is not an int value"); 
         // Here catch NumberFormatException
         // So input is not a int.
       } 
      

      【讨论】:

      • 为什么要使用Boolean?你应该使用boolean
      【解决方案3】:

      如果用户输入是String,那么您可以尝试使用parseInt 方法将其解析为整数,当输入不是有效的数字字符串时抛出NumberFormatException

      try {
      
          int intValue = Integer.parseInt(stringUserInput));
      }(NumberFormatException e) {
          System.out.println("Input is not a valid integer");
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用 try-catch 块来检查整数值

        例如:

        字符串形式的用户输入

        try
        {
           int num=Integer.parseInt("Some String Input");
        }
        catch(NumberFormatException e)
        {
          //If number is not integer,you wil get exception and exception message will be printed
          System.out.println(e.getMessage());
        }
        

        【讨论】:

          【解决方案5】:
                  String input = "";
                  int inputInteger = 0;
                  BufferedReader br    = new BufferedReader(new InputStreamReader (System.in));
          
                  System.out.println("Enter the radious: ");
                  try {
                      input = br.readLine();
                      inputInteger = Integer.parseInt(input);
                  } catch (NumberFormatException e) {
                      System.out.println("Please Enter An Integer");
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  float area = (float) (3.14*inputInteger*inputInteger);
                  System.out.println("Area = "+area);
          

          【讨论】:

            【解决方案6】:

            使用 Integer.parseIn(String),您可以将字符串值解析为整数。如果输入字符串不是正确的数字,您还需要捕获异常。

            int x = 0;
            
            try {       
                x = Integer.parseInt("100"); // Parse string into number
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            

            【讨论】:

            • 我是初学者,看不懂你的代码。你能解释一下吗?
            • 另外,您可能希望将区域从 int 更改为 double。与 int 一样,您将失去精度值。
            • 是的,这似乎有点复杂。您可以使用扫描仪读取的是字符串。 @DarkKnight 在这里所做的,它试图将字符串转换为整数( parseInt 行)。 try/catch 语义只是用来通知程序是否崩溃。这意味着程序会尝试一些事情,如果它崩溃,它会做一些事情。
            • @Fabinout 非常感谢您清楚说明。
            猜你喜欢
            • 2018-05-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-29
            • 2016-05-19
            相关资源
            最近更新 更多