Helpful Information
 
 
Category: Java
Cant find variables?

I am working on this program in which im trying to gather information about certain things. But when i try to compile it says a variable is not found, even though it is clearly defined. Its not quite completely funciontal yet so... bare with it.

Also i am jsut learnign about inheritances so likely its a problem with the way its declared.

also i know there is a lot commented out, but i did that after realize that i think i did it wrong.

Its saying grad year cannot be found but all the other variable from the parent class work. Any Suggestions?

class HighSchool extends Project8Driver
{
public HighSchool()
{
int gradYear;
System.out.print("What year do you plan to graduate? ");
gradYear = scan.nextInt();
System.out.print("How many credits do you need to aquire before you can graduate? ");
int credLeft = scan.nextInt();
System.out.print("What did you score on the SAT? ");
int SAT = scan.nextInt();
System.out.print("How many AP credits do you have? ");
int apCred = scan.nextInt();

}

public String toString()
{
return "Name: " +name+ "\nAge: "+age+ "\nID: "+id+"\n Graduation year: " + gradYear;
}

}



import java.util.Scanner;
class Project8Driver
{
static int age, id;
static double gpa;
static String schoolName, name;
static Scanner scan = new Scanner(System.in);
public static void main(String[]args)
{
// gather general info
/* System.out.print("Please enter your name: ");
name = scan.next();
System.out.print("Please enter your age: ");
age = scan.nextInt();
System.out.print("Please enter your school's name: ");
schoolName = scan.next();
System.out.print("Please enter your ID: ");
id = scan.nextInt();
System.out.print("Please enter your current GPA: ");
gpa = scan.nextDouble();*/

int userChoice = 1;
while (userChoice != 0)
{
menu();
userChoice = scan.nextInt();
menuSwitch(userChoice);
}

}

public static void menuSwitch(int choice)
{
int num;
switch(choice)
{
case 4:
/* int gradYear;
System.out.print("Please enter your expected graduation year: ");
gradYear = scan.nextInt(); */
MiddleSchool mStudent = new MiddleSchool();
System.out.println(mStudent);

break;
case 1:
/*int gradYear, numOfCreditsRemaining, satScore, apCredits;
System.out.print("Please enter your expected graduation year: ");
gradYear = scan.nextInt();
System.out.print("Please enter the number of credits remaining for you to graduate: ");
numOfCreditsRemaining = scan.nextInt();
System.out.print("Please enter your SAT score: ");
satScore = scan.nextInt();
System.out.print("Please enter your number of AP credits: ");
apCredits = scan.nextInt(); */
HighSchool hsStudent = new HighSchool();
System.out.println(hsStudent);
case 2:
/*boolean time;
int pTime, status;
String major, minor;
System.out.print("Please enter your major: ");
major = scan.next();
System.out.print("Please enter your minor: ");
minor = scan.next();
System.out.print("Are you a full time or part time student? (1 - full time, 2 - part time) ");
pTime = scan.nextInt();
if (pTime == 1)
time = true;
else
time = false;
System.out.print("Are you an Undergratuate or Graduate? (1 - Graduate, 2 - undergraduate) ");
status = scan.nextInt();
if (status == 1)
{
System.out.print("What type of degree are you aquiring? ");
String degreeType = scan.next();
System.out.print("What was your score on the GRE? ");
int greScore = scan.nextInt();
}
else if (status == 2)
{
System.out.print("Please enter your exam 1 grade: ");
int examOne = scan.nextInt();
System.out.print("Please enter your exam 2 grade: ");
int examTwo = scan.nextInt();
// create for loop to gather grades and put them in an array
for (int i = 1; i <= 14; i++)
{
System.out.print("Please enter Activity grade for Activity " + i +" : ");
//array[i-1] = scan.nextInt();
}
for (int j = 1; j <= 14; j++)
{
System.out.print("Please enter Exercise grade for Exercise "+j+" : ");
//exerciseArray[j-1] = scan.nextInt();
}
for (int k = 1; k <= 14; i++)
{
System.out.print("Please enter project grade for Project " + k +" : ");
// arrayProjects[k-1] = scan.nextInt();
} */
break;

default:
System.out.println("Invalid choice");
}
}

public static void menu()
{
System.out.print("Please enter your name: ");
name = scan.next();
System.out.print("Please enter your age: ");
age = scan.nextInt();
System.out.print("Please enter your school's name: ");
schoolName = scan.next();
System.out.print("Please enter your ID: ");
id = scan.nextInt();
System.out.print("Please enter your current GPA: ");
gpa = scan.nextDouble();
System.out.println("\tWhat type of stuent are you? ");
System.out.println("*************");
System.out.println("0: Middle School");
System.out.println("1: High School");
System.out.println("2: College");
System.out.println("3: Quit");
System.out.print("\nEnter your choice: ");
}

}

Because you've declared the variables locally to their methods. When you declare a variable like this:
class MyClass {
void MyFunc() {
int myVar = 5;
}
}myVar is only accessible within MyFunc(). If want myVar to be accessible to all methods in the class, you have to make it a property of the class:
class MyClass {
int myVar = 5;

void MyFunc() {
}
}

That makes sense and it is working now. Thanks Twey










privacy (GDPR)