-5

Trying to do if statements and getting the error "Y cannot be resolved to a variable"

System.out.println("Hotel Tax System");
    System.out.println("Custom Tax Rate Y/N");
    Answer = S.nextLine();

    if (Answer = Y); { //getting issue with this line
        System.out.println("Specify customer tax rate:");
        CustomTax = S.nextInt();
        } else {
        System.out.println("Pressummed tax rate 20%");
        }

Anyone know of a potential fix for this solution?

E97
  • 5
  • 4
  • 2
    Dupe: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java and you need to learn the difference between `Y` and `'Y'`. – Marc B Oct 19 '16 at 16:42
  • If you're trying to compare to the character, it should be `'Y'`, not `Y`. `Y` by itself is a name, which it can't resolve to any known entity. Also, you should be using `==`, not `=`; the former compares two entities, and the latter assigns the entity on the right to the one on the left. – Justin Time - Reinstate Monica Oct 19 '16 at 16:43
  • @MarcB: Or indeed, between `Y` and `"Y"`. – T.J. Crowder Oct 19 '16 at 16:45
  • Please let us see where S was defined and Answer as well if S is an object of Scanner then S.nextLine() will surely return a String and if Y is a string literal you will need to place it around double quotes "" else you will need to show the definition of Y as well – Ghost Worker Oct 19 '16 at 16:46
  • @ΦXocę 웃 Пepeúpa ツ At least get rid of the incorrect `;`, as people pointed out when you posted that as an answer? – T.J. Crowder Oct 19 '16 at 17:11

2 Answers2

3

Three mistakes:

  1. Remove the semicolon after the if statement's ()
  2. Put Y in double quotes
  3. Answer = Y is wrong, = is an assignment statement and also used in other expression. If you want to compare strings, don't use == for comparing strings use the equals() or equalsIgnoreCase methods.

So for instance:

if (Answer.equals("Y"){
0

There are two problems with your current code. 1 is that you probably meant "Y" not Y. The compiler is looking for a variable called Y which it can't find. I believe that you meant to compare it to the String "Y" (correct me if I'm wrong ). The second problem is that after your if statement you have a ; meaning to end the if statement and ultimately ignoring the code inside the scope

Try this.

if (Answer.equals("Y")) { 
    System.out.println("Specify customer tax rate:");
    CustomTax = S.nextInt();
} else {
    System.out.println("Pressummed tax rate 20%");
}
paul
  • 96
  • 9
  • I realized that after I submitted it. But give that he accepted my answer rathe than the other two one of which pointed out all three problems, I assume he was just looking for correct code (could be wrong). – paul Oct 19 '16 at 17:16
  • There's an "edit" link you can use to improve your answer. :-) Remember, it's not just for the OP now, it's for others in the future. It's *primarily* for others in the future. (This question is a bad example, but that's the goal of SO.) – T.J. Crowder Oct 19 '16 at 17:52