Im sure its a really easy answer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chachtastic
    New Member
    • Nov 2009
    • 2

    Im sure its a really easy answer

    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.Array IndexOutOfBound sException: 3
    at Max1.main(Max1. java:11)


    here is my program:
    Code:
        public static void main(String[] args){
    
            int max = Integer.MIN_VALUE;
            int var = Integer.parseInt(args[args.length]);
            int x[] = new int[var];
    
            for (int i = 0; i < args.length; i++) {
    
                if(x[i] > max)
                    max = x[i];
    
            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?
    Last edited by Frinavale; Nov 4 '09, 09:41 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    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

    Code:
    int var = Integer.parseInt(args[args.length]);
    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.

    Comment

    • chachtastic
      New Member
      • Nov 2009
      • 2

      #3
      Code:
      public class Max1 {
         
          public static void main(String[] args){
      
              int max = Integer.MIN_VALUE;
              int var = Integer.parseInt(args[args.length]);
              int x[] = new int[var];
      
              for (int i = 0; i < args.length; i++) {
      
                  if(x[i] > max)
                      max = x[i];
      
              System.out.print(max);
          }
      }
      
      }
      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.

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        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.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I think what pbrockway2 is getting at is that arrays are 0 bound.
          Say you have the following array:
          Code:
          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 "firstEleme nt",
          • myArray[1] will give you "secondElement" ,
          • and myArray[2] will give you "thridEleme nt"


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

          Therefore if you have:
          Code:
          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):
          Code:
          String theLastElement = myArray[myArray.length-1];
          So, to fix your problem, change your code to:

          Code:
          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

          Comment

          Working...