This program is suppose to allow the user to enter a student's name and (4) scores via GUI, instead of Scanner class. The class is to use a String array for 5 names, an array of 5 characters to hold letter grades, and 5 arrays of four doubles for each test score.

PROBLEMS:
1) When compiling, I get error: cannot find symbol (twice) for LINE 27
2) I'm only able to enter 1 set of data: 1 student and 4 scores, instead of 5 sets. And, therefore, output is only for 1 student.
3) I need to input/display data via simple GUI.

Can anyone lend some assistance? It would be greatly appreciated.

import java.util.Scanner; // Needs changed to javax.swing.JOptionPane
public class GraBook
{
public static void main(String[] args)
{

final int STUDENTS = 5;// Number of students
final char LETTERS = 5; // Number of grades
final int TESTS = 4; // Number of tests

// Create an array to hold students names, grades & scores
char[] grades = new char[LETTERS];
String[] names = new String[5];
double[] score1 = new double[TESTS];
double[] score2 = new double[TESTS];
double[] score3 = new double[TESTS];
double[] score4 = new double[TESTS];
char grade;

Scanner input = new Scanner(System.in); //Replace w/GUI

// Get the names of each student
for (int index=0; index < names.length; index++)
{
System.out.println("Enter student's name: " + 
	(index + 1) + ": ");
	String[index]= keyboard.nextString();  //Replace w/GUI code


// Get student scores
System.out.println("Insert 4 test scores:");
double[] scores ={input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble()};


// Average 4 test scores:
double average = 0;
for (int i = 0; i < scores.length; i++)
{
average += scores[i];
}
average /= scores.length;

if (average >= 90)
grade = 'A';
else if (average >= 80)
grade = 'B';
else if (average >= 70)
grade = 'C';
else if (average >= 60)
grade = 'D';
else
grade = 'F';

System.out.println(names + " has a(n) " + grade + " with an average test score of " + average); // Replace w/GUI code
}
}
}

This program is suppose to allow the user to enter a student's name and (4) scores via GUI, instead of Scanner class. The class is to use a String array for 5 names, an array of 5 characters to hold letter grades, and 5 arrays of four doubles for each test score.

PROBLEMS:
1) When compiling, I get error: cannot find symbol (twice) for LINE 27
2) I'm only able to enter 1 set of data: 1 student and 4 scores, instead of 5 sets. And, therefore, output is only for 1 student.
3) I need to input/display data via simple GUI.

Can anyone lend some assistance? It would be greatly appreciated.

import java.util.Scanner; // Needs changed to javax.swing.JOptionPane
public class GraBook
{
public static void main(String[] args)
{

final int STUDENTS = 5;// Number of students
final char LETTERS = 5; // Number of grades
final int TESTS = 4; // Number of tests

// Create an array to hold students names, grades & scores
char[] grades = new char[LETTERS];
String[] names = new String[5];
double[] score1 = new double[TESTS];
double[] score2 = new double[TESTS];
double[] score3 = new double[TESTS];
double[] score4 = new double[TESTS];
char grade;

Scanner input = new Scanner(System.in); //Replace w/GUI

// Get the names of each student
for (int index=0; index < names.length; index++)
{
System.out.println("Enter student's name: " + 
	(index + 1) + ": ");
	String[index]= keyboard.nextString();  //Replace w/GUI code


// Get student scores
System.out.println("Insert 4 test scores:");
double[] scores ={input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble()};


// Average 4 test scores:
double average = 0;
for (int i = 0; i < scores.length; i++)
{
average += scores[i];
}
average /= scores.length;

if (average >= 90)
grade = 'A';
else if (average >= 80)
grade = 'B';
else if (average >= 70)
grade = 'C';
else if (average >= 60)
grade = 'D';
else
grade = 'F';

System.out.println(names + " has a(n) " + grade + " with an average test score of " + average); // Replace w/GUI code
}
}
}

In the future, if you quote a line number, you need to point out what line number it is. Highlight it in red or something.

String[index]= keyboard.nextString();

String is a type, not a variable name. You want a variable name here. Do you mean:

names[index] = keyboard.nextString();

Sorry about not highlighting the compile error. When I make the proposed change, it still won't compile: cannot find symbol at
keyboard.nextString()
^

Well, I replaced the troublesome code with the following:
names [index] = input.next();

Now, I am able to enter 5 names and their associated scores. However, my output doesn't display the name I've entered. Instead, I get the undesired output in red below:

Ljava.lang.String;@ca0b6 has a(n) "C" with an average test score of 79.75

Any suggestions as I am new at this and quite frustrated...

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Thats cause "names" is an Array of String objects and not a String itself.
If you need to display the contents of "names" use a loop like:-

for(int i=0;i<names.length;i++) {
  System.out.println(names[i]);
}

BTW you seriously need to learn how to indent your code, this should help you get started.

Thanks - BUT, now my output has increased to 5 lines of the same data.

Was I to add it after the if,else statements like such?

else
grade = 'F';

[INDENT][/INDENT]for(int i=0;i<names.length;i++)
{
[INDENT][/INDENT]System.out.println(names[index] + " has a(n) " + grade + " with an [INDENT][/INDENT]average test score of " + average); // Replace w/GUI code
}
}
}
}

Well, two steps closer. I eliminated the repitious output by omitting for(int i=0;i<names.length;i++) and then leaving the names[index] in the println code.

And I got my validation to work. YIPPI!!!!

If I could only figure out how to convert from Scanner class to JOptionPane input/output...

If you would have indented your code even a little bit, I would have not forgotten the for (int index=0; index < names.length; index++) loop at the beginning of your code.

Anyways This or this should help you get started on JOptionPanes.

Both links have almost the same content just the presentation is different.

Thank you for your suggestions, as these are good resources; however, I've already searched these links and can't find answers to my errors that follow:

ERROR: inputValue is already in main(java.lang.String[]), ERROR: cannot find symbol, pointing to "input" in all five instances

import javax.swing.JOptionPane; // Needed for GUI
public class JoptionPaneTester
	{
	public static void main(String[] args)
	{

	final int STUDENTS = 5;// Number of students
	final char LETTERS = 5; // Number of grades
	final int TESTS = 4; // Number of tests

	// Create an array to hold students names, grades & scores
	char[] grades = new char[LETTERS];
	String[] names = new String[5];
	double[] score1 = new double[TESTS];
	double[] score2 = new double[TESTS];
	double[] score3 = new double[TESTS];
	double[] score4 = new double[TESTS];
	char grade;


	// Get the names of each student
	for (int index=0; index < names.length; index++)
	{	
	String inputValue = JOptionPane.showInputDialog("Enter student's name: " + 
	(index + 1) + ": ");
	names [index] = input.next();  //Replace w/GUI code


	// Get student scores
	String inputValue = JOptionPane.showInputDialog("Insert 4 test scores:");
	double[] scores ={input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble()};


	//Validate test scores
	while (scores[index] < 1.0 || scores[index] > 100.0)
	{
	JOptionPane.showMessageDialog(null, "Invalid input. " +
	"Please enter a number in the range of " +
	"1 through 100.");
	}

	// Average 4 test scores:
	double average = 0;
	for (int i = 0; i < scores.length; i++)
	{
	average += scores[i];
	}
	average /= scores.length;

	if (average >= 90)
	grade = 'A';
	else if (average >= 80)
	grade = 'B';
	else if (average >= 70)
	grade = 'C';
	else if (average >= 60)
	grade = 'D';
	else
	grade = 'F';

	JOptionPane.showMessageDialog(null,"Student # " + (index + 1)+ ": " + names[index] +
		", has a(n) " + grade + " grade with an average test score of " + 
		average); // Replace w/GUI code
}
}
}

The errors are quite clear. You are trying to use "input" but you have not defined it, and you are trying to redeclare "String inputValue" when you have already declared it a few lines above.

I've renamed inputValue to inputName and inputScores, which got rid of one error. Now, I only have the "input.next" errors @ 5.

Any other suggestions? Thanks in advance!

names [index] = input.next();

The above code was valid when you were retrieving your data from the console, so now that should be changed as you are retrieving your value from the Input dialog box.

Its pretty simple you should actually have this:-

names [index] =   JOptionPane.showInputDialog("Enter student's name: " +
    (index + 1) + ": ");

Actually, I was able to solve this first one, but the one that is stumping me is the scores[]...

Is it similar?

Actually, I was able to solve this first one, but the one that is stumping me is the scores[]...

Is it similar?

Yep, its good to see you are learning :) .

Actually, I was able to solve this first one, but the one that is stumping me is the scores[]...

Is it similar?

You need to ask a much more precise question. Post your updated code, post what you're trying to do, and post what problems you are having.

I'm no closer, only get more and more errors...

Do I start with the following:

// Get student scores
	scores [index] = JOptionPane.showInputDialog(null,"Insert 4 test scores: " +
	(index + 1) + ": ");

Entering multiple scores here is tripping me up...

I'm no closer, only get more and more errors...

Do I start with the following:

"Insert 4 test scores: " + (index + 1) + ": ");

Looks like you're adding a String to an integer to a String and trying to get a String. Look at the Integer class and use the valueOf or toString function. Repost your ENTIRE updated code and explain what you are trying to do again and what you've solved, what you haven't etc. There have been too many changes from the original posting to guess what you have now and what your problem is now aside from what i mentioned above.

These are the compile errors I am getting now...

JoptionPaneTester.java:27: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
scores [index] = JOptionPane.showInputDialog(null,"Insert 4 test scores: " +
^
JoptionPaneTester.java:32: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
while (scores[index] < 1.0 || scores[index] > 100.0)
^
JoptionPaneTester.java:32: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
while (scores[index] < 1.0 || scores[index] > 100.0)
^
ϼÏJoptionPaneTester.java:42: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
for (int i = 0; i < scores.length; i++)
^
JoptionPaneTester.java:44: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
average += scores;
^
JoptionPaneTester.java:44: inconvertible types
found : <nulltype>
required: double
average += scores;
^
JoptionPaneTester.java:46: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
average /= scores.length;
^
7 errors

JoptionPaneTester.java:27: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
scores [index] = JOptionPane.showInputDialog(null,"Insert 4 test scores: " +
^
JoptionPaneTester.java:32: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
while (scores[index] < 1.0 || scores[index] > 100.0)
^
JoptionPaneTester.java:32: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
while (scores[index] < 1.0 || scores[index] > 100.0)
^
ϼÏJoptionPaneTester.java:42: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
for (int i = 0; i < scores.length; i++)
^
JoptionPaneTester.java:44: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
average += scores;
^
JoptionPaneTester.java:44: inconvertible types
found : <nulltype>
required: double
average += scores;
^
JoptionPaneTester.java:46: cannot find symbol
symbol : variable scores
location: class JoptionPaneTester
average /= scores.length;
^
7 errors

I'll repeat. You say you've changed your code, so you need to post all of it again.

sorry, I was researching and trying, but now I don't know what I'm doing for sure.

import javax.swing.JOptionPane; // Needed for GUI
public class JoptionPaneTester
	{
	public static void main(String[] args)
	{

	final int STUDENTS = 5;// Number of students
	final char LETTERS = 5; // Number of grades
	final int TESTS = 4; // Number of tests

	// Create an array to hold students names, grades & scores
	char[] grades = new char[LETTERS];
	String[] names = new String[5];
	double[] score1 = new double[TESTS];
	double[] score2 = new double[TESTS];
	double[] score3 = new double[TESTS];
	double[] score4 = new double[TESTS];
	char grade;

	// Get the names of each student
	for (int index=0; index < names.length; index++)
	{	
	names [index] = JOptionPane.showInputDialog(null,"Enter student's name: " + 
	(index + 1) + ": ");

	// Get student scores
	scores [index] = JOptionPane.showInputDialog(null,"Insert 4 test scores: " );
	scores [index] = Double.parseDouble(scores [index]);


	//Validate test scores
	while (scores[index] < 1.0 || scores[index] > 100.0)
	{
	JOptionPane.showMessageDialog(null, "Invalid input. " +
	"Please enter a number in the range of " +
	"1 through 100.");
	}


	// Average 4 test scores:
	double average = 0;
	for (int i = 0; i < scores.length; i++)
	{
	average += scores[i];
	}
	average /= scores.length;

	if (average >= 90)
	grade = 'A';
	else if (average >= 80)
	grade = 'B';
	else if (average >= 70)
	grade = 'C';
	else if (average >= 60)
	grade = 'D';
	else
	grade = 'F';

	JOptionPane.showMessageDialog(null,"Student # " + (index + 1)+ ": " + names[index] +
		", has a(n) " + grade + " grade with an average test score of " + 
		average); // Replace w/GUI code
}
}
}

Reread my last post regarding adding strings to integers, but you have no variable called scores defined anywhere and you are trying to use it, so you need to declare it before you use it. on the other hand, i don't see anywhere where you use score1, score2, score3, score4, so i assume those are obsolete and you're changing it to a scores[] array, but you've done it halfway. You need to have one or the other. Java has no idea what scores[] is since you haven't told it what it is. Declare it and initialize it with the new command before you use it. if you aren't using score1, score2, score3, score4, get rid of them.

Besides the string array and the array for 5 chars for letter grades, the class was to be written with five arrays of four doubles each to hold each student's set of test scores. I take it that I went about this last part wrong?

Besides the string array and the array for 5 chars for letter grades, the class was to be written with five arrays of four doubles each to hold each student's set of test scores. I take it that I went about this last part wrong?

You have a "class" in name only. You are doing everything in your main function. Your JoptionPaneTester class has no data members and no methods. Everything is defined in your main function, which is static and so you couldn't access any data members from it if you wanted to. I would take a good hard look at the assignment and make sure you aren't supposed to have all of these variables defined outside of main. Having everything in main pretty much defeats the entire Object-Oriented nature of Java, but perhaps the instructor wants it all in main now and will explain how and why to organize it differently later. I don't know what you are supposed to do in this assignment, but make sure you aren't supposed to create a class called Student or something and have an array of type Student.

Or perhaps you are supposed to have a 2-dimensional array called scores, where scores[2][3] represents the score of student 2 on test 3 or something. I just don't know. That's how I'd do it though. If you aren't going to create a Student class, I'd just create a 2-dimensional score array and get rid of the score1, score2, score3, score4 arrays. I don't see why you'd want or need them. But if it's an assignment, you have to do it as specified. Just make sure you understand exactly what is required. Can't think of any more advice to give without seeing the assignment specifications, but if you are going to use scores[], you need to define it.

Here are the exact instructions-
Write a class that uses a string array object to hold 5 students' names,
an array of 5 chars to hold the 5 students' letter grades,
and 5 arrays of 4 doubles each to hold each student's set of scores.
The class should have methods that return a specific student's name, avg. test score, and a letter grade based on the avg.

Demo the class in a program that allows user to enter each student's name and coinciding test scores. It should then display each student's avg. test score and letter grade.

Input validation: do not accept scores < 0 or > 100.

Our chapter covers 2D arrays, which I thought would be a good approach, but our author does a poor job explaining them and he does not exemplify w/GUIs and we are required to use GUIs.

I know my formatting (indenting) needs work as I've been told several times in these threads, but honestly, I am more concerned with grasping the programming concepts and will continue to read up/practice formatting best practices.

*This is the 2nd time I've attempted to reply to your comments from a couple days ago, but my replies aren't always submitted, successfully.

First of all the assignment is very wrong. The idea is to learn OOP. As other have understood your teacher should have said to create an object Student with the elements required and have an array of Students.

The way your code is written I would suggest all of your arrays will habe size 5.

grades[5] : you will store the average as a char
names[5] : the names of the 5 students
score1[5] : the score of Test 1 for the 5 students
score2[5] : the score of Test 2 for the 5 students
score3[5] : the score of Test 3 for the 5 students
score4[5] : the score of Test 4 for the 5 students

Anyway here is your code with my suggestions on reading and checking the scores:

for (int index=0; index < names.length; index++)
	{	
	names [index] = JOptionPane.showInputDialog(null,"Enter student's name: " + 
	(index + 1) + ": ");

       boolean correct=false;
       
        while (!correct) {
           double dScore = JOptionPane.showInputDialog(null,"Insert Score 1 for student "
  + index +": " );
           if (dScore  >= 0.0 && dScore  <= 100.0){
              score1[index] = dScore;
              correct = true;
           } else {
              JOptionPane.messageDialog(null,"Error try again");
           }
         }

correct=false;
while (!correct) {
           double dScore = JOptionPane.showInputDialog(null,"Insert Score 2 for student "
  + index +": " );
           if (dScore  >= 0.0 && dScore  <= 100.0){
              score2[index] = dScore;
              correct = true;
           }
}

correct=false;
while (!correct) {
           double dScore = JOptionPane.showInputDialog(null,"Insert Score 3 for student "
  + index +": " );
           if (dScore  >= 0.0 && dScore  <= 100.0){
              score3[index] = dScore;
              correct = true;
           }
}

correct=false;
while (!correct) {
           double dScore = JOptionPane.showInputDialog(null,"Insert Score 4 for student "
  + index +": " );
           if (dScore  >= 0.0 && dScore  <= 100.0){
              score4[index] = dScore;
              correct = true;
           }
      }

 } //for loop

Now that you have all the scores you can calculate the average for each student from the arrays score1,2,3,4 and put the right letter to the grades

It would have been better if you had one 2-D array for the scores. In that way you could avoid the 4 while( !correct ) and have another loop, but I thought to use your code.

But If you teacher really wanted you learn java he would have asked you to create a Student object with attributes the scores and methods for calculating the average and the you could create an array of Students


I know my formatting (indenting) needs work as I've been told several times in these threads, but honestly, I am more concerned with grasping the programming concepts and will continue to read up/practice formatting best practices.

They go hand in hand. If you don't format, you won't be able to tell where blocks of code start and end and your errors will go up astronomically, as will the time and effort required to find them. Time spent formatting is gained back tenfold when debugging.

First of all the assignment is very wrong. The idea is to learn OOP. As other have understood your teacher should have said to create an object Student with the elements required and have an array of Students.

The way your code is written I would suggest all of your arrays will habe size 5.

grades[5] : you will store the average as a char
names[5] : the names of the 5 students
score1[5] : the score of Test 1 for the 5 students
score2[5] : the score of Test 2 for the 5 students
score3[5] : the score of Test 3 for the 5 students
score4[5] : the score of Test 4 for the 5 students

I would just add that I would declare all of these variables as class data members, which means define them outside of any function, and in particular, define them outside of main. This quote seems to suggest that that is what is desired, though I'm not completely positive:

The class should have methods that return a specific student's name, avg. test score, and a letter grade based on the avg.

So you would need a Constructor and you'd take all of the code out of main and put it in the Constructor and have main be one line that calls the Constructor.

That's my best guess, but I'd ask for some clarification from the instructor as to what he/she wants in main. Probably nothing.

This article has been dead for over six months. Start a new discussion instead.