Reputation: 1014
I am trying to print the name of the student in this array. I keep getting an error message that says,
[Ljava.lang.String;@55f96302
I declare and initialize the Name near the top and I am trying to print it with "System.out.println("The average scores of student "+Names+" is:"+avg);" but obviously the having Names alone is not the solution. How can I fix my code?
import java.util.Scanner;
public class Grades {
//An array to hold the student names
String[] Names = new String[6];
//An array to hold students letter grades
char[] LetterGrades = new char[6];
//an array to hold each of the students test scores
double[] Test1Scores = new double[6];
double[] Test2Scores = new double[6];
double[] Test3Scores = new double[6];
void EnterData() {
//initialize the scanner to parse the user inputs
Scanner sc = new Scanner(System.in);
//initialize the for loop and create iterations from 0 to 6
for (int i = 0; i < 6; i++) {
//get user's name
System.out.print("Enter the name of student " + (i + 1) + ": ");
Names[i] = sc.next();
//Create a while loop to get each of the students test scores
do {
System.out.print("Enter the score for Test 1: ");
Test1Scores[i] = sc.nextDouble();
} while (Test1Scores[i] < 0 || Test1Scores[i] > 100);
do {
System.out.print("Enter the score for Test 2: ");
Test2Scores[i] = sc.nextDouble();
} while (Test2Scores[i] < 0 || Test2Scores[i] > 100);
do {
System.out.print("Enter the score for Test 3: ");
Test3Scores[i] = sc.nextDouble();
} while (Test3Scores[i] < 0 || Test3Scores[i] > 100);
System.out.println("Testscores: " + Test1Scores[i] + " " + Test2Scores[i] + " " + Test3Scores[i]);
}
}
void GetNames() {
for (int i = 0; i < 6; i++)
System.out.println((i + 1) + ". " + GetName(i + 1));
}
String GetName(int studNumber) {
return (Names[studNumber - 1]);
}
//prints the 3 test scores of the student
void GetTestScores(int studNumber) {
System.out.println(GetTestScore(studNumber, 1) + " ");
System.out.println(GetTestScore(studNumber, 2) + " ");
System.out.println(GetTestScore(studNumber, 3) + " ");
}
double GetTestScore(int studNumber, int testNumber) {
if (testNumber == 1)
return (Test1Scores[studNumber - 1]);
else if (testNumber == 2)
return (Test2Scores[studNumber - 1]);
else if (testNumber == 3)
return (Test3Scores[studNumber - 1]);
return 0;
}
//calculate the average test score
double GetAverageScore(int studNumber) {
double avg = (Test1Scores[studNumber - 1] + Test2Scores[studNumber - 1] + Test3Scores[studNumber - 1]) / 3;
System.out.println("The average scores of student "+Names+" is:"+avg);
return avg;
}
void GetLetterGrades() {
for (int i = 1; i <= 6; i++) {
System.out.println("The letter grade of student " + (i) + " is: " + GetLetterGrade(i));
}
}
char GetLetterGrade(int studNumber) {
double sum = Test1Scores[studNumber - 1] + Test2Scores[studNumber - 1] + Test3Scores[studNumber - 1];
char letterGrade;
if (sum / 3 >= 90)
letterGrade = 'A';
else if (sum / 3 >= 80)
letterGrade = 'B';
else if (sum / 3 >= 70)
letterGrade = 'C';
else if (sum / 3 >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}
void GetClassAverage() {
double sum = 0.0;
for (int i = 0; i < 6; i++)
for (int j = 0; j < 3; j++)
sum += GetTestScore(i + 1, j + 1);
System.out.println("The average of the class is: " + sum / 18);
}
void GetTopGrade() {
char letterGrade;
double top = GetAverageScore(1);
for (int i = 2; i <= 6; i++)
if (GetAverageScore(i) > top)
top = GetAverageScore(i);
if (top >= 90)
letterGrade = 'A';
else if (top >= 80)
letterGrade = 'B';
else if (top >= 70)
letterGrade = 'C';
else if (top >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
System.out.println("The top score of the class is: " + letterGrade);
}
}
Upvotes: 0
Views: 1417
Reputation: 1014
Thanks for the help but I was able to get it to work with the following:
System.out.println("The average scores of student " + Names[studentNumber - 1] + " is:"+avg);
Upvotes: 0
Reputation: 61
If you want to print Names of students in the string like The average scores of student a,b,c is: 100
You may create a method which returns comma separated values of the input String array by iterating over the elements and using that method in: "The average scores of student "+csvStringMethod(Names)+" is:"+avg
Upvotes: 1
Reputation: 46
In the example you provided, you are printing the array itself and not the values in the array. You need to iterate through the array 'Names' and print out each value individually.
for( int i = 0;i<Names.length;i++){
System.out.println(Names[i]);
}
Upvotes: 1
Reputation: 589
What you want to print is the name of the student, but the variable Names is the array type of it.
So what you will want to do is to change
System.out.println("The average scores of student "+Names+" is:"+avg);
into
System.out.println("The average scores of student "+Names[studNumber]+" is:"+avg);
this.
Upvotes: 2