user3353167
user3353167

Reputation: 892

recognize string as input and throw exception if not an integer

I'm new to java and was trying to do this program. Basically entering 3 numbers, it will calculate the volume of a cube. If a negative number is typed then it will throw an exception, and also when there are more then 3 input. I wanted it to throw an exception also, if the input is not a number, but I have no idea how to store the input inside a variable and then check if it's a string and eventually throw an exception. Any suggestions? Here's my code

     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       

Upvotes: 1

Views: 21847

Answers (2)

Manoj Shrestha
Manoj Shrestha

Reputation: 4684

You can create your own exception class and throw the instance of that class from a method.

The exception class:

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

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

And for parsing the input string to integer, call a method like this :

int width = parseInt(args[0]);

where your parseInt() method throws your custom exception as follows:

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

Now, you can catch your custom exception MyException similar to other standard exceptions:

       // 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

Upvotes: -1

jgitter
jgitter

Reputation: 3414

This:

int width = Integer.parseInt(args[0]);

already throws a NumberFormatException if the String in question is not a valid string representation of an integer.

EDIT:

To address your comments:

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
   }
}

Upvotes: 3

Related Questions