Connecting Tech Pros Worldwide Forums | Help | Site Map

Im sure its a really easy answer

Newbie
 
Join Date: Nov 2009
Posts: 2
#1: 3 Weeks Ago
I can get my program to compile but when I try to run it I get this message:

java Max1 2 51 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Max1.main(Max1.java:11)


here is my program:
Expand|Select|Wrap|Line Numbers
  1.     public static void main(String[] args){
  2.  
  3.         int max = Integer.MIN_VALUE;
  4.         int var = Integer.parseInt(args[args.length]);
  5.         int x[] = new int[var];
  6.  
  7.         for (int i = 0; i < args.length; i++) {
  8.  
  9.             if(x[i] > max)
  10.                 max = x[i];
  11.  
  12.         System.out.print(max);

I know that it means im trying to reach an index that is unavailable but I just dont know how to make it work so that a user types in however many numbers he wants, and the program outputs the largest number value.

I know this is probably really simple, but help?

Member
 
Join Date: Nov 2007
Posts: 32
#2: 3 Weeks Ago

re: Im sure its a really easy answer


That code won't compile.

I think you have a typo (a missing couple of }), but it's a really code idea to cut and paste your code and use the code button so that we can see exactly what the real line 11 is.

You have

Expand|Select|Wrap|Line Numbers
  1. int var = Integer.parseInt(args[args.length]);
  2.  
That line is always going to generate an AIIOOBE because args.length is the length of the array of arguments so that array runs from 0 to args.length-1.

In fact you don't have to parse anything to get the desired length of the x array: just make it have length args.length. What you do have to parse is each of the string elements of the args array so that you get int values that you can put into the x array.

Only once you have populated the x array with int values can you use the for loop to figure out the maximum value.
Newbie
 
Join Date: Nov 2009
Posts: 2
#3: 3 Weeks Ago

re: Im sure its a really easy answer


Expand|Select|Wrap|Line Numbers
  1. public class Max1 {
  2.  
  3.     public static void main(String[] args){
  4.  
  5.         int max = Integer.MIN_VALUE;
  6.         int var = Integer.parseInt(args[args.length]);
  7.         int x[] = new int[var];
  8.  
  9.         for (int i = 0; i < args.length; i++) {
  10.  
  11.             if(x[i] > max)
  12.                 max = x[i];
  13.  
  14.         System.out.print(max);
  15.     }
  16. }
  17.  
  18. }
  19.  
That is my full program that I am working on. I have tried running it without parsing but then I get another error about it being incompatible.
Member
 
Join Date: Nov 2007
Posts: 32
#4: 3 Weeks Ago

re: Im sure its a really easy answer


args[args.length] will throw an AIOOBE. And it will do so for any array args. Every time.

The solution is to initialise x to be an int array of length args.length

If you are getting "another error" then you have to fix that other error.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#5: 3 Weeks Ago

re: Im sure its a really easy answer


I think what pbrockway2 is getting at is that arrays are 0 bound.
Say you have the following array:
Expand|Select|Wrap|Line Numbers
  1. String myArray[] = {"firstElement","secondElement","thirdElement"};
Since myArray has 3 elements, myArray.length will return 3.

The thing is that arrays in Java are 0 based. This means that the first element in the array resides at index 0.

So,
  • myArray[0] will give you "firstElement",
  • myArray[1] will give you "secondElement",
  • and myArray[2] will give you "thridElement"

If you try to access myArray[3] you will get an error because the array index 3 doesn't exist.

Therefore if you have:
Expand|Select|Wrap|Line Numbers
  1. String theLastElement = myArray[myArray.length];
You will get an exception. To retrieve the last element properly (without exception) you should be accessing the element at myArray.length-1 (which is the index of the last element):
Expand|Select|Wrap|Line Numbers
  1. String theLastElement = myArray[myArray.length-1];
So, to fix your problem, change your code to:

Expand|Select|Wrap|Line Numbers
  1. int var = Integer.parseInt(args[args.length-1]);
This will give you the last element in the "args" array.

(Please be aware that if the last element in the args array is not an Integer you will experience a different type of error....)

-Frinny
Reply