Results 1 to 6 of 6
  1. #1
    itmajor1990 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Exclamation arrays and for loop java program coding

    Hello,

    I'm in a beginning class for java programming and I can't seem to understand how to use the array and for loop together. I keep getting this message when I compile my program

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at GradeList.main(GradeList.java:43)


    Here is my coding thus far

    Java Code:
    public class GradeList
    { 
            public static void main(String[] args)
            { // main method
    	  // tell the user this program calculates grades for a class
    	  System.out.println("This program calculates grades for a class of students");
              // declare variables
              int numStudents; 
    	  int totalpts; 
    	  int studentpts;
    	  int k;
    	  double percent;
    	  char grade;
    	  // input scanner
    	  Scanner input = new Scanner(System.in);
    
    	  // ask and get users input number of students and total possible points
    	  System.out.print("Please Enter the Number of students: ");
    	  numStudents = input.nextInt();
    	  System.out.print("Please enter the Total Possible Points: ");
    	  totalpts = input.nextInt();
    
    	  // tell user to enter each students points
    	  System.out.println("Please Enter Each Students Points");
    	  System.out.println(" ");
    
    	  // declare array
    	  int [] studentList = new int [numStudents];
    
    	  // input for loop
    	  for (k=0; k <= numStudents; k++)
    	  {
    		  System.out.print("Points for Student " +k+1+ " ");
    		  studentpts = input.nextInt();
    
    	  while (studentpts > totalpts)
    	  { // loop if user enters points greater than total 
    
    		  // tell user invalid student points
    		  System.out.println("Sorry that is over the total possible, try again");
    
    		  // ask user again for student points
    		  System.out.print("Points for student " +k+ " ");
    		  studentpts = input.nextInt();
    	  }
    	  	  studentList[k] = studentpts;
    
    	  }
    
    	  // print formatted output for headings
    	  String formathd= "CLASS OF " +numStudents+ " STUDENTS              POINTS     PERCENT     GRADE  ";
    
    	  // format lines
    	  String formatlns= "                                 ------     -------     -----  ";
    
    	  // show variables and lines
    	  System.out.println(formathd);
    	  System.out.println(formatlns);
    
              // calculate percent and grade for each student points and store into percent array
    	  for (k=0; k < numStudents; k++)
    	  {
    		  percent = studentList[k] *100;
    	  if (percent >= 90.00 
    	  	{
    	  grade = 'A';
    	  	}
    	  else if (percent >= 80.00)
    	  	{
              grade = 'B';
    	  	}
    	  else if (percent >= 70.00)
    	  	{
              grade = 'C';
    	  	}
    	  else if (percent >= 60.00)
              	{
              grade = 'D';
    	  	}
    	  else
      	  	{
    		grade= 'F';
    		}
    	  String format= "%25s      %11d     %11.2f     %9c";
    	  System.out.printf(format, studentList[k], percent, grade);
    	  }
    
      
            } // end of main method
    } // end of class
    Last edited by itmajor1990; 04-27-2012 at 10:15 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: arrays and for loop java program coding

    Java Code:
    // declare array
    int [] studentList = new int [numStudents];
     
    // input for loop
    for (k=0; k <= numStudents; k++;)
    {
    
        // ...
    
        studentList[k] = studentpts;
    }
    This doesn't look right to me. The for loop goes to high: when k equals numStudents then studentlist[k] will be out of bounds.

  3. #3
    itmajor1990 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: arrays and for loop java program coding

    I changed it to

    Java Code:
    	  // input array
    	  int [] studentList = new int [numStudents];
    
    	  // input for loop
    	  for (k=0; k < numStudents; k++)
    	  {
    		  System.out.print("Points for Student " +k+1+ " ");
    		  studentList[k] = input.nextInt();
    
    	  while (studentList[k] > totalpts)
    	  { // loop if user enters points greater than total 
    
    		  // tell user invalid student points
    		  System.out.println("Sorry that is over the total possible, try again");
    
    		  // ask user again for student points
    		  System.out.print("Points for student " +k+ " ");
    		  studentList[k] = input.nextInt();
    	  }
    	  }
    It still gives me same error.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: arrays and for loop java program coding

    Could you confirm that the exception occurs on the "studentList[k] = input.nextInt();" line (or lines)?

    Try printing the values of k and numStudents just before the exception occurs to check that they have the values you think they should have.

  5. #5
    itmajor1990 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: arrays and for loop java program coding

    This is how it prints once I compile and run my coding:

    java GradeList
    This program calculates grades for a class of students
    Please Enter the Number of students: 5
    Please enter the Total Possible Points: 380
    Please Enter Each Students Points

    Points for Student 1 327
    Points for Student 2 397
    Sorry that is over the total possible, try again
    Points for student 2 297
    Points for Student 3 356
    Points for Student 4 246
    Points for Student 5 295
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at GradeList.main(GradeList.java:43)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: arrays and for loop java program coding

    That output doesn't seem to come from that code. (In the code one prompt uses k+1 the other uses k). Also the exception line number is the same - which may just be a coincidence, but it's suspicious. Perhaps you're running old code.

    Also, do check (with System.out.println()) the values of k and numStudents just before the exception occurs.

Similar Threads

  1. Battleship Program Coding Assistance
    By nekaneka19 in forum New To Java
    Replies: 7
    Last Post: 03-21-2012, 07:26 PM
  2. Help with java program while loop
    By sfmikejava in forum New To Java
    Replies: 7
    Last Post: 09-29-2011, 12:25 AM
  3. Java Program: Grading using Arrays [Help]
    By Sly Cooper in forum New To Java
    Replies: 1
    Last Post: 01-30-2011, 08:22 AM
  4. Adding Arrays and Enhanced For Loop into program.
    By vinyacam in forum New To Java
    Replies: 1
    Last Post: 05-10-2010, 07:28 AM
  5. Help with program coding
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •