Variable Cannot Be Resolved

Variable cannot be resolved

The problem is variable scoping.

if (someCondition) {
final int i = 666;
} else {
final int i = 42;
}
int j = i + 1; // compile-time error

Here we have two local variables i who goes out of scope immediately after they're declared and initialized. If j needs the value of i, then i would have to be declared in a larger scope.

final int i;
if (someCondition) {
i = 666;
} else {
i = 42;
}
int j = i + 1; // compiles fine!

(It should be mentioned that this is exactly the kind of scenarios where the ternary operator excels, i.e.)

final int i = (someCondition) ? 666 : 42;

In your specific case, unfortunately the array initializer shorthand can only be used to initialize upon declaration. That is:

int[] arr1 = { 1, 2, 3 }; // compiles fine!
int[] arr2;
arr2 = { 4, 5, 6 }; // doesn't compile!

You can pull out the declaration of items outside the if and write the verbose code for each case (see Joachim Sauer's answer), but a more concise code is to use array-of-arrays instead.

final CharSequence[][] allItems = {
{ "4:45", "5:00" },
{ "4:43", "4:58" },
{ "4:41", "4:56" },
{ "4:38", "4:53" }
};
final CharSequence[] items = allItems[j];

This technique works well in this case, but in the more general case you want to use a Map or something similar.

Note: It's not explicit in the original code, but this works if j can either be 0, 1, 2, or 3. If you want the last option to apply when j is any value other than 0, 1, 2, then you have to check for that and set it to 3 before this code.

variable cannot be resolved ( variable used in if else statement)

The problem is with your implementation. As you see you define k in the start. But the interesting part is you define it as nothing:

int k;

The other k you have defined are inside the if and else if function and not accessible to the println() function. That means you are passing something that is undefined to be printed. That would surely result in an error. This is called scope as XtremeBaumer said. Meaning it is the property of your variables that define where they are visible and usable.

The correct way to do it would be this:

double a = 2.1223 ;
int b = (int) a ;

if(a-b > 0.5) {
k = b + 1;
}

else {
k = b; //No casting needed
}

System.out.println(k);

That way you are not creating a new k variable every time you check with the if, or else if. What you are doing is using that k variable again by just saying k = whatever. Your problem was that you created the k variable again by doing int k.

Also, as Andy Turner pointed out you don't need to cast b to int because it already is an int.

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
}
}

finally block - variable cannot be resolved

From a technical perspective - a variable declared in the try argument isn't available in the finally clause, as you've seen. The good news here is that from a function perspective - finish() shouldn't be in the finally block anyway. finish is part of the positive (a.k.a "happy") flow, and should only be called when you're done writing to the stream. In other words, if the write operation failed and an exception was thrown, you shouldn't call finish anyway.

To make a long story short - move the finish call inside the try block:

Side note: Since your method throws an IOException, there's no reason to catch the exception and rethrow it. You can clean up the code by allowing it to be thrown from the method call directly:

private void createFile(final String json) throws IOException {
final String fileName = getConfigFileName(this.getSomeFile());
try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(fileName + ".gz"))) {
out.write(json.getBytes());
out.finish();
}
}

Confused about the warning Cannot resolve variable

You have 2 options:

Option 1

<% String username = (String) request.getAttribute("username"); %>
<%= username %>

Option 2

${requestScope.username}


Related Topics



Leave a reply



Submit