I'm getting an error when ever my program reaches this for loop and tries to run it
for (int m = 0; m < students; m++){
allScores = allScores + scores[m];
students was declared as an int allScores was declared as a double as was the scores[] array.
This is the error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Lab4.main(Lab4.java:51)
line 51 is the for for (int m = 0; m < students; m++) line
Edit:
This is my entire code, i may not have done the scores[] array correctly:
import java.util.*;
import java.text.*;
public class Lab4 {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
String input;
int students;
int d=1;
double allScores = 0;
char [] answerKey= { 'B' , 'D' , 'A' , 'A' , 'C' , 'A' , 'B' , 'A' , 'C' , 'D' , 'B' , 'A' };
char [] userAnswers = new char[answerKey.length];
DecimalFormat df = new DecimalFormat("#0.0");
System.out.print("how many students are in the class? ");
input = s.nextLine();
students=Integer.parseInt(input);
String [] name = new String[students];
double [] scores = new double[students];
for (int j = 0; j < students; ++j)
{
int correctAnswers=0;
System.out.print("Enter name of student " + (j + 1) + ": ");
name[j] = s.nextLine();
System.out.print("Enter quiz score answers :");
String line = s.nextLine().toUpperCase();
for (int k = 0; k < answerKey.length; k++) {
userAnswers[k] = line.charAt(k);
}
for (int i = 0; i < userAnswers.length; i++)
{
if(userAnswers[i] == answerKey[i])
{
correctAnswers++;
}
}
System.out.println(name[j]);
System.out.println((df.format((((float)(correctAnswers) / answerKey.length)) * 100)) + "%");
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
d++;
}
System.out.println("the high score is " + "and the low score is " );
for (int m = 0; m < scores.length; m++){
allScores = allScores + scores[m];
}
System.out.println("Average is:" + ((allScores/students)));
}
}
EDIT:
Sorry I read the lines wrong line 51 was
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
Edit:
new code no longer giving me error messages, but I can't seem to get the averaging of scores to work.
import java.util.*;
import java.text.*;
public class Lab4 {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
String input;
int students;
int d=1;
double allScores = 0;
char [] answerKey= { 'B' , 'D' , 'A' , 'A' , 'C' , 'A' , 'B' , 'A' , 'C' , 'D' , 'B' , 'A' };
char [] userAnswers = new char[answerKey.length];
DecimalFormat df = new DecimalFormat("#0.0");
System.out.print("how many students are in the class? ");
input = s.nextLine();
students=Integer.parseInt(input);
String [] name = new String[students];
double [] scores = new double[students + 1];
for (int j = 0; j < students; ++j)
{
int correctAnswers=0;
System.out.print("Enter name of student " + (j + 1) + ": ");
name[j] = s.nextLine();
System.out.print("Enter quiz score answers :");
String line = s.nextLine().toUpperCase();
for (int k = 0; k < answerKey.length; k++) {
userAnswers[k] = line.charAt(k);
}
for (int i = 0; i < userAnswers.length; i++)
{
if(userAnswers[i] == answerKey[i])
{
correctAnswers++;
}
}
System.out.println(name[j]);
System.out.println((df.format((((float)(correctAnswers) / answerKey.length)) * 100)) + "%");
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
d++;
}
System.out.println("the high score is " + "and the low score is " );
for (int m = 0; m < scores.length; m++){
allScores = allScores + scores[m];
}
System.out.println("Average is:" + ((allScores/students)));
}
}
The problem is that students
is initialized to a value that is greater than scores.length
. You can use scores.length
instead of students
as the upper limit for the loop; alternatively, initialize students
to scores.length
(or something lower than it).
If you just want to iterate through the entire array, you can use the enhanced for loop syntax:
for (double score : scores) {
allScores = allScores + score;
}
EDIT I counted lines myself (kind of tedious) and I land on this line:
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
What's happening is that d
gets too large. That's probably because you initialize d
to 1 instead of 0.
Use length property in array for iteration.
for (int m = 0; m < scores.length; m++){
allScores = allScores + scores[m];
However, you mentioned that the error is happened in line 51, but line 51 is :
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
So your d is equals or larger than scores.length.
I'm curious about why you intialize d with an 1?
change it to 0 may solve the problem.
int d=0;
Another problem in your code is:
((float)((correctAnswers) / answerKey.length) * 100);
change it to
(((float)correctAnswers / answerKey.length) * 100);
Otherwise, you change the result, which is already an int to float. That will impact the result.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
This is because some where you are accessing array beyond its limit. may be in scores[m];
you can use following:
for (int m = 0; m < scores.length; m++){
allScores = allScores + scores[m];
there are some irregularities with your code:
int d=1; // your score[index] starts with this just change to 0;
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
//your scores length will now be d + students; use the students instead
for (int m = 0; m < scores.length; m++){ //for (int m=0;m<students;m++)
allScores = allScores + scores[m];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.