Reputation: 7
I'm trying to reference the GPA in my StudentData class, however I'm getting an error that says it can't find the symbol on the line that says aStudent.gpa = studentGPA.
my main class:
public static void main (String [] args)
{
//local constants
final String QUIT = "Quit";
final String YES = "Y";
final int MODIFY_GPA = 1;
final int DISPLAY_USER = 2;
final int QUIT_MENU = 3;
//local variables
String name; //name of the user
String idPrompt; //asks the user if they want to input a gpa and id
String studentID; //student ID input by the user
float studentGPA; //student GPA input by the user
int choice; //prompts the user for a menu choice
Library myLib = new Library();
/****************** Start main method *******************/
//prompt for name of user or quit
System.out.print("Enter name of user(First and Last, Quit to end): ");
name = Keyboard.readString();
while(!QUIT.equals(name))
{
//ask the user if they want to enter ID and GPA
System.out.print("Do you want to enter an ID and GPA?(Y/N): ");
idPrompt = Keyboard.readString();
//if the user says yes
if(!YES.equals(idPrompt))
{
//instantiate a new Student with just the name
StudentData aStudent = new StudentData(name, "", 0);
}
else
{
//prompt the user for the ID
System.out.print("Enter the Student ID: ");
studentID = Keyboard.readString();
//prompt the user for the GPA
System.out.print("Enter the Student GPA: ");
studentGPA = Keyboard.readFloat();
//instantiate a new Student with all three data types
StudentData aStudent = new StudentData(name, studentID, studentGPA);
}
//clear the screen
myLib.clrscr();
//prompt user for a menu choice
choice = displayMenu();
//clear the screen
myLib.clrscr();
//begin while loop to modify the user or display
while(choice != QUIT_MENU)
{
//if the user wants to modify the GPA
if(choice == MODIFY_GPA)
{
studentGPA = StudentData.modifyGPA();
aStudent.gpa = studentGPA;
}
//if the user wants to display
else if(choice == DISPLAY_USER)
{
System.out.print("STUDENT OUTPUT");
//System.out.println(StudentData);
}
//if there was an invalid menu choice
else
{
System.out.print("INVALID DATA ENTERED");
}
//prompt for next choice
choice = displayMenu();
}
//prompt for name of user or quit
System.out.print("Enter name of user(First and Last, Quit to end): ");
name = Keyboard.readString();
}
}//end main method
and this is my StudentData class public class StudentData { public String name; public String id; public float gpa;
//create a constructor that will receive the name of the student only
public StudentData(String inName)
{
//local variables
id = ""; //set the ID to null
gpa = 0.00F; //set the gpa to 0.00
name = inName; //gets the name of the student from the user
}
//create an overloaded constructor that will receive all three pieces of instance data
public StudentData(String inName, String inID, float inGPA)
{
name = inName; //name input by user through the constructor parameter
id = inID; //ID input by user through the constructor parameter
gpa = inGPA; //GPA input by user through constructor parameter
}
//create a method that will modify the students GPA
public static float modifyGPA()
{
//local constants
//local variables
float newGPA;
System.out.print("Enter new GPA: ");
newGPA = Keyboard.readFloat();
return newGPA;
}
//create a toString method that will format the instance data to look like:
public String toString()
{
String format;
format = ("Student" + name + "\n" +
"Student ID" + id + "\n" +
"Student GPA" + gpa + "\n");
return format;
}
Upvotes: 0
Views: 88
Reputation: 187
for your posted problem,you can do like this in main class,
// your code
StudentData aStudent = null;
while(!QUIT.equals(name))
{
//your code
//if the user says yes
if(!YES.equals(idPrompt))
{
//instantiate a new Student with just the name
aStudent = new StudentData(name, "", 0);
}
else
{
//your code
//instantiate a new Student with all three data types
aStudent = new StudentData(name, studentID, studentGPA);
}
//your code
Upvotes: 0
Reputation: 47739
Scope, scope, scope!! Declarations in Java (and C/C++) are "scoped" to the "block", where a "block" is a set of statements between {
and }
.
This means that if you declare a variable { StudentData aStudent = new StudentData(...); }
it goes "poof" when you pass that closing }
. If you want it to be "visible" outside that scope you need to declare aStudent
outside the block.
Note that you can still do the assignment inside that block, just don't mistakenly include StudentData
or you will be declaring an entirely different version of the variable. And note that, in Java, a variable must be assigned a value along all possible paths from its declaration to its use, so you may have to assign null
(eg) to the variable where it's declared.
Upvotes: 1
Reputation: 285405
It's the studentGPA
variable. It's declared in a local block:
if(!YES.equals(idPrompt))
{
// variable *declared* here!
StudentData aStudent = new StudentData(name, "", 0);
// aStudent is visible here
}
// but it's not visible here out of the scope of this local block.
and thus only visible in that block. If you want it visible throughout the method, then declare it in method or class scope.
Upvotes: 2