Confused on error in array.. (Java)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bkquinn359
    New Member
    • Apr 2008
    • 1

    #1

    Confused on error in array.. (Java)

    Hi All,
    I am getting the follwoing error

    Exception in thread "main" java.lang.Array IndexOutOfBound sException: 0
    at Chp8Assign1.mai n(Chp8Assign1.j ava:37)
    Press any key to continue...

    In this program...

    public class Chp8Assign1 {
    public static void main (String args [ ] )
    {
    int myArray [ ] = new int [ args.length ]; // for loop to convert String args array to integer myArray
    int gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0;
    for (int a = 0; a < args.length ; a++)
    {
    myArray [a ] = Integer.parseIn t (args [ a ] );
    if(myArray [a] >= 90)
    {
    gradeA++;
    }
    else if(myArray [a] >= 80 && myArray [a] <= 89)
    {
    gradeB++;
    }
    else if(myArray [a] >= 70 && myArray [a] <= 79)
    {
    gradeC++;
    }
    else if(myArray [a] >= 60 && myArray [a] <= 69)
    {
    gradeD++;
    }
    else if( myArray [a] <= 60)
    {
    gradeF++;
    }
    }
    int sum = 0;

    int largest = myArray[0];
    int smallest = myArray[0];
    // for loop to find sum, largest and smallest
    for (int i = 0; i<myArray.lengt h; i++ )
    {
    sum = sum + myArray [i]; if ( myArray [ i ] > largest )
    largest = myArray [ i ]; if ( myArray [ i ] < smallest ) smallest = myArray [ i ];
    }
    System.out.prin tln("The sum is " + sum);
    System.out.prin tln("The average is " + sum/myArray.length) ;
    System.out.prin tln("The largest number is " + largest);
    System.out.prin tln("The smallest number is " + smallest);
    System.out.prin tln("The number of students with scores of 90-100 (A) is " + gradeA);
    System.out.prin tln("The number of students with scores of 80-89 (B) is " + gradeB);
    System.out.prin tln("The number of students with scores of 70-79 (C) is " + gradeC);
    System.out.prin tln("The number of students with scores of 60-69 (D) is " + gradeD);
    System.out.prin tln("The number of students with scores below 60 (F) is " + gradeF);
    } // ends main
    }


    Could anyone point me in the right direction?

    Thanks.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by bkquinn359
    Could anyone point me in the right direction?

    Thanks.
    You're defining an array as long as the number of arguments you supplied on
    the command line. You didn't supply any arguments so you end up with an
    array having 0 (zero) elements.

    kind regards,

    Jos

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by bkquinn359
      Hi All,
      I am getting the follwoing error

      Exception in thread "main" java.lang.Array IndexOutOfBound sException: 0
      at Chp8Assign1.mai n(Chp8Assign1.j ava:37)
      Press any key to continue...

      In this program...

      public class Chp8Assign1 {
      public static void main (String args [ ] )
      {
      int myArray [ ] = new int [ args.length ]; // for loop to convert String args array to integer myArray
      int gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0;
      for (int a = 0; a < args.length ; a++)
      {
      myArray [a ] = Integer.parseIn t (args [ a ] );
      if(myArray [a] >= 90)
      {
      gradeA++;
      }
      else if(myArray [a] >= 80 && myArray [a] <= 89)
      {
      gradeB++;
      }
      else if(myArray [a] >= 70 && myArray [a] <= 79)
      {
      gradeC++;
      }
      else if(myArray [a] >= 60 && myArray [a] <= 69)
      {
      gradeD++;
      }
      else if( myArray [a] <= 60)
      {
      gradeF++;
      }
      }
      int sum = 0;

      int largest = myArray[0];
      int smallest = myArray[0];
      // for loop to find sum, largest and smallest
      for (int i = 0; i<myArray.lengt h; i++ )
      {
      sum = sum + myArray [i]; if ( myArray [ i ] > largest )
      largest = myArray [ i ]; if ( myArray [ i ] < smallest ) smallest = myArray [ i ];
      }
      System.out.prin tln("The sum is " + sum);
      System.out.prin tln("The average is " + sum/myArray.length) ;
      System.out.prin tln("The largest number is " + largest);
      System.out.prin tln("The smallest number is " + smallest);
      System.out.prin tln("The number of students with scores of 90-100 (A) is " + gradeA);
      System.out.prin tln("The number of students with scores of 80-89 (B) is " + gradeB);
      System.out.prin tln("The number of students with scores of 70-79 (C) is " + gradeC);
      System.out.prin tln("The number of students with scores of 60-69 (D) is " + gradeD);
      System.out.prin tln("The number of students with scores below 60 (F) is " + gradeF);
      } // ends main
      }


      Could anyone point me in the right direction?

      Thanks.
      There is nothing wrong with your code....

      except that you tend to execute it without initial values at execution....

      Code:
      java Chp8Assign1
      That may lead to ArrayIndexOutOf BoundsException ....
      Some codes are depending on args values... If none, you have to put a trap on your code.... or you could use try/catch block....

      Code:
      java Chp8Assign1 98 89 95 97 92
      regards,
      sukatoa

      Comment

      Working...