-2
import java.util.Scanner;
public class Application {

public static void main(String[] args) {
    do {
    System.out.println("What is the command keyword to exit a loop in Java?\na. int\nb. continue\nc. break\nd. exit\nEnter your choice");
    Scanner ans = new Scanner(System.in);
    char ans = sc.next().charAt(0);
    if(ans=='c')
        System.out.println("Correct");
    else
        System.out.println("Wrong! Presss Y to try again.");
    Scanner redo = new Scanner(System.in);
    char redo = sc.next().charAt(0);
    }
    while(redo=='y');
}

}

I'm just starting to learn Java, can you please tell me what's wrong in my code and how to improve it? Thanks

This is the error I receive.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Duplicate local variable ans
sc cannot be resolved
Duplicate local variable redo
sc cannot be resolved
redo cannot be resolved to a variable

at Application.main(Application.java:9)
8
  • Why do you think it's wrong?
    – Joe C
    Sep 24, 2017 at 21:01
  • it doesn't work. Here is the error... Exception in thread "main" java.lang.Error: Unresolved compilation problems: Duplicate local variable ans sc cannot be resolved Duplicate local variable redo sc cannot be resolved redo cannot be resolved to a variable at Application.main(Application.java:9) Sep 24, 2017 at 21:02
  • Please don't post errors in comments. Please edit your question instead.
    – Joe C
    Sep 24, 2017 at 21:03
  • I've edited my question Sep 24, 2017 at 21:05
  • Good. Now what part of these error messages do you find unclear?
    – Joe C
    Sep 24, 2017 at 21:05

2 Answers 2

1

Think you want to get smth like this:

public class Application {

    public static void main(String[] args) {
        char redo;
        do {
            System.out.println("What is the command keyword to exit a loop in Java?\na. int\nb. continue\nc. break\nd. exit\nEnter your choice");
            Scanner scanner = new Scanner(System.in);
            char ans = scanner.next().charAt(0);
            if (ans == 'c') {
                System.out.println("Correct");
                break;
            }
            else {
                System.out.println("Wrong! Presss Y to try again.");
                redo = scanner.next().charAt(0);
            }
        }
        while (redo == 'y');
    }

}

Problems in your implementation:

Wrong variable definition

Attempt to redefine ans variable leads to the compilation error. Use different variable names. For ex:

Scanner scanner = new Scanner(System.in);
char ans = scanner.next().charAt(0);

instead of this

Scanner ans = new Scanner(System.in);
char ans = sc.next().charAt(0);

Probably you want to break the loop if answer is correct

So better to add break when ans=='c':

if (ans == 'c') {
   System.out.println("Correct");
   break;
}

While condition variable definition

Define redo variable before do-while loop block, otherwise you'll get compilation error

3
  • It leads to this error .. The local variable redo may not have been initialized Sep 24, 2017 at 21:38
  • I've initialized it to zero. Thanks a lot for your help ! Do you know why it should be set to zero first? Sep 24, 2017 at 21:40
  • If you check redo value without break statement from my proposal, it should be initialized with some value (smth but not 'y', otherwise it breaks the logic). But if you use break statement when ans=='c', than you'll get of the loop before redo checking (so in that case redo='0' definition is not necessary) Sep 24, 2017 at 21:46
0

Duplicate local variable "ans" in the following 2 statement. rename anyone of then to another.

Scanner ans = new Scanner(System.in);
   char ans = sc.next().charAt(0);
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.