Results 1 to 6 of 6
- 04-27-2012, 09:13 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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 classLast edited by itmajor1990; 04-27-2012 at 09:15 PM.
- 04-27-2012, 09:33 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: arrays and for loop java program coding
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.Java Code:// declare array int [] studentList = new int [numStudents]; // input for loop for (k=0; k <= numStudents; k++;) { // ... studentList[k] = studentpts; }
- 04-27-2012, 09:37 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: arrays and for loop java program coding
I changed it to
It still gives me same error.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(); } }
- 04-27-2012, 09:44 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-27-2012, 09:48 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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)
- 04-27-2012, 10:06 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
Battleship Program Coding Assistance
By nekaneka19 in forum New To JavaReplies: 7Last Post: 03-21-2012, 06:26 PM -
Help with java program while loop
By sfmikejava in forum New To JavaReplies: 7Last Post: 09-28-2011, 11:25 PM -
Java Program: Grading using Arrays [Help]
By Sly Cooper in forum New To JavaReplies: 1Last Post: 01-30-2011, 07:22 AM -
Adding Arrays and Enhanced For Loop into program.
By vinyacam in forum New To JavaReplies: 1Last Post: 05-10-2010, 06:28 AM -
Help with program coding
By cachi in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 07:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks