3 Replies - 424 Views - Last Post: 28 February 2016 - 05:29 PM Rate Topic: -----

#1 jckool18  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 27-February 16

Array out of bounds question

Posted 28 February 2016 - 05:07 PM

I am having trouble with a line of code in my program. It is giving me an error that says array out of bounds in my command line argument. Is there anyone that can take a look at this section of code and tell me what I am missing or what I need to add>

    public static void main(String[] args) throws IOException { 
		System.out.println( "\ncommand line arguments:\n");
		for ( int i = 0; i < args.length; i++ ) { 
			String s = args[ i ]; 
			System.out.println( "argument " + i + ": " + s ); 
		}
		try {
		File file = new File( args[ 0 ] );
		int intvalue1 = Integer.parseInt( args[ 1 ] );
		System.out.println( "\nintvalue = " + intvalue1);
		int intvalue2 = Integer.parseInt( args[ 2 ] );
		System.out.println( "\nintvalue = " + intvalue2);

			BufferedReader br = new BufferedReader( new FileReader( file ));
			System.out.println( "\nFile opened successfully.");
			br.close(); 
		} catch ( Exception e ) { e.printStackTrace(); 
		}
    	}


Is This A Good Question/Topic? 0
  • +

Replies To: Array out of bounds question

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: Array out of bounds question

Posted 28 February 2016 - 05:19 PM

Quote

error that says array out of bounds in my command line argument.

Use the array's length field to test that the array has enough entries for the index you want to use.
if(theArray.length < 4) {
  //  can't use an index of 3 or greater here
}


Quote

It is giving me an error

Please copy the full text of the error message and paste it here. It has important info about the error.

This post has been edited by NormR: 28 February 2016 - 05:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 jckool18  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 27-February 16

Re: Array out of bounds question

Posted 28 February 2016 - 05:27 PM

So do i need to have an if statement with the File and array inside of the if statement

View PostNormR, on 28 February 2016 - 05:19 PM, said:

Quote

error that says array out of bounds in my command line argument.

Use the array's length field to test that the array has enough entries for the index you want to use.
if(theArray.length < 4) {
  //  can't use an index of 3 or greater here
}


Quote

It is giving me an error

Please copy the full text of the error message and paste it here. It has important info about the error.



this is the error

command line arguments:

java.lang.ArrayIndexOutOfBoundsException: 0
at hillcipher.Hillcipher.main(Hillcipher.java:21)
Was This Post Helpful? 0
  • +
  • -

#4 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: Array out of bounds question

Posted 28 February 2016 - 05:29 PM

Quote

java.lang.ArrayIndexOutOfBoundsException: 0
That says there is no element at index 0. If there isn't an element at index 0 then the array is empty. The code needs to test the array's length before trying to use an index into it that is past the end of the array.

This post has been edited by NormR: 28 February 2016 - 05:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1