Smurf1
Smurf1

Reputation: 21

Variable cannot be resolved, error in Eclipse but not BlueJ

Yes there are plenty of duplicates to this question, I know.

The following code has no problems compiling in BlueJ, however, when ran in Eclipse I get the error on the following line:

while (selection != 'Q' && selection != 'q');

The error is: selection cannot be resolved to a variable.

Why do I experience this error in Eclipse and not BlueJ?

public class menuMain {

    public menuMain(String args) {

        System.out.println("Welcome to the Project");
        Scanner in = new Scanner(System.in);
        do {
            showMenu();
            String menu = in.nextLine(); // read a line of input
            char selection;

            if (menu.length() > 0) {
                selection = menu.toLowerCase().charAt(0); // extract the first char of the line read
            }
            else  {
                System.out.println("invalid input:\t"+selection);
                System.out.println("Press enter to continue...");
                Scanner itScan = new Scanner(System.in);
                String nextIt = itScan.nextLine();
            } 
            switch (selection)
            {
                case 'a':
                changeTime time = new changeTime ();
                break;

                case 'b':
                watchTime timeStop = new watchTime();
                break;

                case 'q':
                System.out.println("\nEnding Now\n");
                System.exit(0);
                break;

                default: 
                System.out.println("Instruction is invalid");
            }
        } 
        while (selection != 'Q' && selection != 'q');    
        System.exit(0);
    }
}

Upvotes: 0

Views: 123

Answers (1)

Alexander Frolov
Alexander Frolov

Reputation: 139

As YCF_L mentioned char selection: is out of scope. When working in Eclipse I usually declare it before do while statement. Do not think to much about it ;-)

Upvotes: 3

Related Questions