Java, "Variable Name" Cannot Be Resolved to a Variable

Java, Variable name cannot be resolved to a variable

If you look at the scope of the variable 'hoursWorked' you will see that it is a member of the class (declared as private int)

The two variables you are having trouble with are passed as parameters to the constructor.

The error message is because 'hours' is out of scope in the setter.

How to fix 'cannot be resolved to a variable' error in Java

You are missing curly braces:

public int[] sort1(int[] a){
for (int i=0; i<a.length;i++) {
for(int j=i+1; j<a.length; j++) {
int min = a[i];
if (a[j] < min) {
a[i] = a[j];
a[j] = min;
min = a[i];
}
}
}
return a;
}

The inner loop has multiple statements, so you must enclose them with curly braces.

While the outer loop has only one statement, it is still advisable to enclose it with curly braces too.

(java) name cannot be resolved to a variable

The problem is you are missing the variable name when instancing the game:

public class GameLauncher{
public static void main(String[] args){
GuessGame game= new GuessGame(); //game is the variable
game.startGame();
}
}

grade cannot be resolved to a variable

You need to assign the grade to the Student object you are passing to the assignGrade method.


public class Teacher {

public String teacherName;

public Teacher() {

System.out.println("Teacher object has been created succesfully!");
}

public void assignGrade(Student alum, int finalGrade) {

alum.grade = finalGrade; // << this is the line I changed
}
}


Related Topics



Leave a reply



Submit