Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to check for shortage of command line arguments

  1. #1
    Member
    Join Date
    May 2011
    Posts
    61
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to check for shortage of command line arguments

    Hi, how do I check for a shortage of command line arguments entered, I tried this in main and it works, but is that proper coding technique?
    Also, I want to know why it doesn's throw the ArrayIndexOutOfBoundsException to indicate too many command line arguments so how do I
    define this, I am confused. Also, please see the commented out if-block, I want to make sure that when user enters
    pos integers greater than the supported range for an int in Java, that it should give a user friendly error message but that
    if-block doesn't run as I tried to compile it, how do I implement this then? (Note: I noticed that the NumberFormatException seems to catch values exceeding the range for pos integers, but I thought NumberFormatException is to check if in a given string there are non numeric characters...

    Btw: the program for the sum method I written (not included) is supposed to sum the three command line arguments and return the int. (the sum method is for adding pos ints only I want)

    public static void main(String[] args)
    {
      if ( args.length < 3 )
        return;
     
    /**HOW DO I CHECK FOR BOUNDARY VALUES THEN IF THE BELOW IF BLOCK DOESN'T WORK...****/
      /if ( Integer.parseInt(args[0]) > Integer.MAX_VALUE | 
       // Integer.parseInt(args[1]) > Integer.MAX_VALUE |	
       // Integer.parseInt(args[2]) > Integer.MAX_VALUE  ) 
       // {
    	  // System.out.println("Int inputs too large, not supported");
              // return;
         //}
     
      try
      {
         sum(Integer.parseInt[0]);
         sum(Integer.parseInt[1]);
         sum(Integer.parseInt[2]); 
      }
      catch (java.lang.ArrayIndexOutOfBoundsException exception) 
      {
    	System.out.println("T");
       }
      catch (NumberFormatException nfe)//to deal with non-int input
     {
            System.out.println("Inputs cannot be non-integers.");
     }
    }

    Any help appreciated.
    Last edited by IHeartProgramming; January 16th, 2012 at 12:14 AM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to check for shortage of command line arguments

    I tried this in main and it works, but is that proper coding technique?
    Yes, it is.
    Also, I want to know why it doesn's throw the ArrayIndexOutOfBoundsException to indicate too many command line arguments so how do I
    define this, I am confused.
    Coz you return it if it has arguments less than 3. Try removing if(args.length<3) and pass two arguments to experience index out of bound exception.

Similar Threads

  1. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  2. command line arguments
    By rizla in forum Member Introductions
    Replies: 3
    Last Post: December 12th, 2010, 11:14 PM
  3. Multi-Valued Command Line Arguments
    By joey86 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 29th, 2009, 11:19 AM
  4. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM
  5. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM