0

The goal at this point is just to find out why this code isn't compiling. The class below creates a new instance of 'K12Student' according to certain conditions, that class being a super class which extends to three subclasses which define different types of students. Each subclass contains unique instance variables and their get and set methods. The class is currently as follows:

import java.util.*; //Create New arraylist for client instances public class StudentInput { private InputHelper input; private ArrayList students; public void run() { studentInfoEntry(); } //Assign data to instances of client public void studentInfoEntry() { students = new ArrayList(); input = new InputHelper(); String studentIDString = ""; int studentID = 0; String studentName = ""; String schoolName = ""; String gradeLevelString = ""; int gradeLevel = 0; String validateAddNewStudent = ""; while (true) { studentIDString = input.getUserInput("Enter student ID number."); studentID = Integer.parseInt(studentIDString); studentName = input.getUserInput("Enter student name."); schoolName = input.getUserInput("Enter school name."); gradeLevelString = input.getUserInput("Enter grade level."); gradeLevel = Integer.parseInt(gradeLevelString); if (gradeLevel >= 0 && gradeLevel <= 12) { if (gradeLevel >= 0 && gradeLevel <= 4) { String readingLevelString = ""; int readingLevel = 0; String classSection = ""; readingLevelString = input.getUserInput("Enter reading level."); readingLevel = Integer.parseInt(readingLevelString); classSection = input.getUserInput("Enter class section."); /*K12Student*/ PrimaryStudent newStudent = new PrimaryStudent(); newStudent.setStudentID(studentID); newStudent.setStudentName(studentName); newStudent.setSchoolName(schoolName); newStudent.setReadingLevel(readingLevel); newStudent.setClassSection(classSection); } if (gradeLevel >= 5 && gradeLevel <= 8) { String lunchHourString = ""; int lunchHour = 0; String homeroomTeacher = ""; lunchHourString = input.getUserInput("Enter lunch hour."); lunchHour = Integer.parseInt(lunchHourString); homeroomTeacher = input.getUserInput("Enter homeroom teacher."); /*K12Student*/ MiddleStudent newStudent = new MiddleStudent(); newStudent.setStudentID(studentID); newStudent.setStudentName(studentName); newStudent.setSchoolName(schoolName); newStudent.setLunchHour(lunchHour); newStudent.setHomeroomTeacher(homeroomTeacher); } if (gradeLevel >= 9 && gradeLevel <= 12) { String GPAString = ""; int GPA = 0; String collegeChoice = ""; GPAString = input.getUserInput("Enter reading level."); GPA = Integer.parseInt(GPAString); collegeChoice = input.getUserInput("Enter class section."); K12Student newStudent = new HighStudent(); newStudent.setStudentID(studentID); newStudent.setStudentName(studentName); newStudent.setSchoolName(schoolName); newStudent.setReadingLevel(readingLevel); newStudent.setClassSection(classSection); } students.add(newStudent); validateAddNewStudent = input.getUserInput("Enter another student? (y/n)"); if (!validateAddNewStudent.equals("y")) { break; } }else{ System.out.println("Grade level must be from 0-12."); } } } } 

Also, the instantiation of each new middle school and elementary school student originally read like that of the high school student (K12Student newStudent = new HighStudent;), but I had more issues compiling with that format. The compiler errors are as follows:

javac K12StudentTestDrive.java

./StudentInput.java:97: error: cannot find symbol newStudent.setReadingLevel(readingLevel); ^ symbol: variable readingLevel location: class StudentInput ./StudentInput.java:98: error: cannot find symbol newStudent.setClassSection(classSection); ^ symbol: variable classSection location: class StudentInput ./StudentInput.java:100: error: cannot find symbol students.add(newStudent); ^ symbol: variable newStudent location: class StudentInput 3 errors 

Thanks in advance for any feedback.

2 Answers 2

1

Variables only live in the scope they are declared in, scope being, for the most part, curly braces (there are other scope rules, but in your case, it's block scope). You define your variables inside the braces of an earlier if statement. They go out of scope as soon as the code leaves that if statement. They are not available later.

The significant parts of your code are:

if (...) { int readingLevel = ...; String classSection = ...; ... } // <= readingLevel and classSection go out of scope here if (...) { K12Student newStudent = ...; newStudent.setReadingLevel(readingLevel); // <= readingLevel isn't here newStudent.setClassSection(classSection); // <= classSection isn't here } // <= newStudent goes out of scope here students.add(newStudent); // <= newStudent isn't here 

Note that this is unrelated to abstract classes.

Sign up to request clarification or add additional context in comments.

4 Comments

As it stands, the variables are defined in the same scope as they are defined in. As for the 'newstudent', is there a way to instantiate it earlier considering that it will be of a different subclass depending upon the conditions in the 'if' statements?
After reassessing the declaration of my variables and moving them far enough back that they can be accessed by all inner functions, I'm still receiving the same error as before. Other factors that could be causing this?
@user3525241 The error "cannot find symbol" means simply that: Whatever symbol you are referring to does not exist in that context. There aren't really any other factors; incorrect scope, misspelled names, variables and fields that don't exist, etc. are frequent causes. So, you missed something in your changes. Look at the line where the error occurs, look through your code to find the definition of the symbol, and determine where the mistake is.
Also if you find yourself randomly rearranging declarations until things work, that's a red flag that your design may be a bit off; scope should reflect your intentions, and should accurately represent the life time of the information that the variable represents.
0

Variables have block level scope in Java.

http://www.java2s.com/Tutorial/Java/0020__Language/VariableScopeinablock.htm

newStudent.setReadingLevel(readingLevel); can't read readingLevel because you declared it in a block, and then exited that block which takes the variable readingLevel out of scope. Essentially it stops existing at that point. If you want the variable to live longer, you'll need to move the declaration outside of the block it's currently in.

1 Comment

I can't seem to find the point where I exited the block before assigning the variable. I think they are all within the same block as the one they are declared in. As for the newStudent; I moved the declaration back a block, but the error is the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.