-1
import java.util.Scanner;

public class MainFile {
    public static void main(String[] args) {    
        do {
            Scanner asc = new Scanner(System.in);
            String userTXT = asc.nextLine();
        } while(userTXT != "Twitter!");     
    }
}

The code is simple. Yet, there's Cannot find symbol for userTXT.
Any tip to avoid such Error is welcomed!

AISAN
  • 1
  • 1
  • 5

1 Answers1

1

It's because you haven't defined or initialised userTXT in a scope that can be seen from within the while loop.

You have defined it within the scope of the while loop which can't see variables defined inside it as the while loop and the variable are not in the same scope ({}).

Maybe try this:

import java.util.Scanner;

public class MainFile {
    public static void main(String[] args) {
        String userTXT = ""; // define it here (not necessarily with "" though)
        do {
            Scanner asc = new Scanner(System.in);
            userTXT = asc.nextLine();
        } while(!userTXT.equals("Twitter!"));
    }
}

Also you should use .equals() instead of != when comparing Strings in Java.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
rhdev
  • 60
  • 6