-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)
  • Why do you think it's wrong? – Joe C Sep 24 '17 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) – Ahmed Refaat Sep 24 '17 at 21:02
  • Please don't post errors in comments. Please [edit] your question instead. – Joe C Sep 24 '17 at 21:03
  • I've edited my question – Ahmed Refaat Sep 24 '17 at 21:05
  • Good. Now what part of these error messages do you find unclear? – Joe C Sep 24 '17 at 21:05
  • I'm really new to programming. I don't know what I should do to fix them – Ahmed Refaat Sep 24 '17 at 21:06
  • @AhmedRefaat the compiler is very helpful in telling you what's wrong. just give the variables different names, also note that `redo` that's used on your `while` condition is not within scope. – Ousmane D. Sep 24 '17 at 21:07
  • Why do the variable names make an error? It is in scope in my code . I just can't edit it in the question – Ahmed Refaat Sep 24 '17 at 21:09

2 Answers2

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

Alex Saunin
  • 716
  • 6
  • 14
  • It leads to this error .. The local variable redo may not have been initialized – Ahmed Refaat Sep 24 '17 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? – Ahmed Refaat Sep 24 '17 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) – Alex Saunin Sep 24 '17 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);
caot
  • 2,274
  • 22
  • 30
  • @AhmedRefaat change Scanner ans into Scanner sc. take time to learn how to use eclipse. Here is a tutorial https://www.youtube.com/watch?v=xO5DpU2j-WE – caot Sep 24 '17 at 21:26
  • @AhmedRefaat a related question and example at link https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java – caot Sep 24 '17 at 21:38