将字符串识别为输入,如果不是整数则抛出异常

时间:2014-02-25 21:12:39

标签: java custom-exceptions

我是java的新手并且正在尝试执行此程序。基本上输入3个数字,它将计算立方体的体积。如果输入负数,那么它将抛出异常,并且当有超过3个输入时也是如此。我希望它也抛出异常,如果输入不是数字,但我不知道如何将输入存储在变量中,然后检查它是否是一个字符串并最终抛出异常。有什么建议?这是我的代码

     public class CubeVolume
     {
       public static void main(String [] args)
       {
         try
         {
           // try if there is more than 3 arguments 
           int width = Integer.parseInt(args[0]);
           int depth = Integer.parseInt(args[1]);
           int hight = Integer.parseInt(args[2]);
           if (args.length > 3)
           throw new ArrayIndexOutOfBoundsException
                 ("You have supplied " + args.length + " arguments!");

          // try if there is less than 3 arguments
          if (args.length < 3)
          throw new ArrayIndexOutOfBoundsException
              ("You have supplied " + args.length + " arguments!");                    

          // checks if the width entered is equal or less than 0
          if (width <= 0)
          throw new NumberFormatException
              ("The argument " + width + " is a negative number!");

          // checks if the depth entered is equal or less than 0
          if (depth <= 0)
          throw new NumberFormatException
              ("The argument " + depth + " is a negative number!"); 

          // checks if the hight entered is equal or less than 0
          if (hight <= 0)
          throw new NumberFormatException
              ("The argument " + hight + " is a negative number!");     


          int volume = width * depth * hight;
          System.out.println("The volume of a cube with dimensions " + "(" + width 
                             + "," + hight + "," + depth + ") " + "is " + volume);
         } // try

        // if there's one than more argument error will be displayed
        catch (ArrayIndexOutOfBoundsException exception)
        {
          System.out.println("Please supply width, depth and hight arguments!");
          System.out.println("Exception message was: '" + exception.getMessage() 
                             + "'");
          System.err.println(exception);
        } // catch          

       // if a negative number is entered error will be displayed
       catch (NumberFormatException exception)
       {
         System.out.println("Dimensions for a cube can't be negative, please "
                                   + "insert only positive whole numbers!");
         System.out.println("Exception message was: '" + exception.getMessage() 
                                   + "'");     
         System.err.println(exception);
       } // catch

     } // main
  } // CubeMain       

2 个答案:

答案 0 :(得分:2)

此:

int width = Integer.parseInt(args[0]);
如果有问题的String不是整数的有效字符串表示,

已经抛出NumberFormatException。

编辑:

解决您的意见:

public class CubeVolume {
   private int width;
   private int depth;
   private int height;

   public static void main(String [] args) {
       if (args.length != 3) {
           throw new Exception("Width, height and depth are required arguments");
       }
       width = Integer.parseInt(args[0]);
       depth = Integer.parseInt(args[1]);
       height = Integer.parseInt(args[2]);

       // more stuff here
   }
}

答案 1 :(得分:-1)

您可以创建自己的异常类,并从方法中抛出该类的实例。

异常类:

// Extending Exception makes your class throwable
class MyException extends Exception {

    public MyException( String string ) {
        super( string );
    }
}

要将输入字符串解析为整数,请调用如下方法:

int width = parseInt(args[0]);

您的parseInt()方法会抛出自定义异常,如下所示:

    private int parseInt( String number ) throws Exception {
        try {
            return Integer.parseInt( number );
        } catch ( Exception e ) {
            throw new MyException( "The input is not a number" );
        }
    }

现在,您可以捕获与其他标准例外类似的自定义异常MyException

       // catching your custom exception
       catch ( MyException e ) {
            System.err.println( e );
        }

        // if there's one than more argument error will be displayed
        catch (ArrayIndexOutOfBoundsException exception)
        {
          System.out.println("Please supply width, depth and hight arguments!");
          System.out.println("Exception message was: '" + exception.getMessage() 
                             + "'");
          System.err.println(exception);
        } // catch          

       // if a negative number is entered error will be displayed
       catch (NumberFormatException exception)
       {
         System.out.println("Dimensions for a cube can't be negative, please "
                                   + "insert only positive whole numbers!");
         System.out.println("Exception message was: '" + exception.getMessage() 
                                   + "'");     
         System.err.println(exception);
       } // catch