Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Variable Error during compiling(Exception in thread "main" java.lang.Error: Unresolved compilation problem: temp cannot be resolve)
Variable Error during compiling [message #1815963] Wed, 16 October 2019 05:56 Go to next message
Lalai Masigon is currently offline Lalai MasigonFriend
Messages: 2
Registered: October 2019
Junior Member
Hi! I'm new to Java using eclipse kepler. I am following a video tutorial and i followed all the syntax correctly but eclipse says "temp temp cannot be resolve to a variable"

My question is: Is it a syntax error or an eclipse version problem?
Glad to have your comments. Thanks.

package Day.examples;

public class ExampleArray {

public static void main(String[] args) {
// TODO Auto-generated method stub

int []x = {10,20,30,40,50};
System.out.println(x[0]+" "+x[3]);

for (int temp : x);
{System.out.println(temp);}

}
}
Re: Variable Error during compiling [message #1815969 is a reply to message #1815963] Wed, 16 October 2019 11:07 Go to previous messageGo to next message
Emmanuel Chebbi is currently offline Emmanuel ChebbiFriend
Messages: 32
Registered: February 2018
Member
Hi,

This is a Java error: you have a semicolon (;) right after the declaration of your for loop.

As a result, the ; is evaluated as an empty statement and is seen as the body of your loop. Meanwhile, your print statement is considered to be outside of the loop and thus cannot access the temp variable.

This is better seen when the code is formatted as follows:
package Day.examples;

public class ExampleArray {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int[] x = { 10, 20, 30, 40, 50 };
        System.out.println(x[0] + " " + x[3]);

        for (int temp : x)
            ;
        {
            System.out.println(temp);
        }
    }
}

[Updated on: Wed, 16 October 2019 11:08]

Report message to a moderator

Re: Variable Error during compiling [message #1816003 is a reply to message #1815969] Thu, 17 October 2019 02:47 Go to previous message
Lalai Masigon is currently offline Lalai MasigonFriend
Messages: 2
Registered: October 2019
Junior Member
Hi sir. Thank you very much for your response. I've followed your instruction and I am still having the same error message. I found the problem by accident in the JRE system library that I am using. The default should be C:\Program files\Java\jdk 1.8.0.221 instead of C:\Program files\Java\jre 1.8.0.231. I remember that I installed java jre 1.80.231 and eclipse switch automatically to the new system library. So, when i switch back to the original library which is java JDK, it works fine.

package Day.examples;

public class ExampleArray {

public static void main(String[] args) {
// TODO Auto-generated method stub

int []x = {10,20,30,40,50};
System.out.println(x[0]+" "+x[3]);
for (int temp : x){
System.out.println(temp);
}

}
}
Previous Topic:Unable to launch Eclipse in Ubuntu
Next Topic:RCP Eclipse Plugin running as Eclipse Application, but Export into running Host makes no changes
Goto Forum:
  


Current Time: Fri Nov 15 15:12:39 GMT 2019

Powered by FUDForum. Page generated in 0.03571 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top