0

Im programming a text adventure, and i am getting these errors:

Error:(59, 45) java: incompatible types: java.lang.String cannot be converted to boolean
Error:(60, 29) java: incomparable types: boolean and char
Error:(60, 36) java: bad operand types for binary operator '&&'
  first type:  boolean
  second type: char
Error:(64, 26) java: incomparable types: java.lang.Boolean and char
Error:(64, 33) java: bad operand types for binary operator '&&'
  first type:  boolean
  second type: char

i am new to java, (about a week of experience) and have tried altering && to ||, changing to boolean and back to string.

switch (firstChoice){
            case "go to mailbox":
                System.out.println("The mailbox is closed. Open it?(y/n");
                userInput.nextLine();
                String mailbox;
                mailbox = userInput.nextLine();
                if (mailbox == 'Y' && 'y')
                System.out.println("The mailbox is rusted shut. Pull harder?");

The output should be The mailbox is rusted shut. Pull harder?

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • `firstChoice` seems to bee a `boolean` and thus can have only values `true` or `false`. You try to compare it with the `String "go to mailbox"`. – Turing85 Jul 27 '19 at 19:04
  • 2
    In your own words, in plain English, what do you expect `mailbox == 'Y' && 'y'` to mean? – Karl Knechtel Jul 27 '19 at 19:07
  • @KarlKnechtel if the user input is Y or y, say that The mailbox is rusted shut. – TheRedRipper Jul 27 '19 at 19:13
  • @TheRedRipper If you have any compilation error, please add your complete code(class). It will help debug the problem. – Bikas Katwal Jul 27 '19 at 19:15
  • Maybe change the ' ' single quotations to double " ", since it's a string you're getting it out of, not a char. However you would need to do a .equals() in that case. – Adan Vivero Jul 27 '19 at 19:17

2 Answers2

6

First, you are comparing a String with == (don't do that) to a character . Second, you want an or (not an and) and finally, it would be cleaner with equalsIgnoreCase. Like,

if (mailbox.equals("Y") || mailbox.equals("y"))

or using Yoda conditions and String.equalsIgnoreCase(String) like

if ("Y".equalsIgnoreCase(mailbox))
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

You can try out if("Y".equalsIgnoreCase(mailbox)) instead of if (mailbox == 'Y' && 'y'), this should work.

knbr
  • 21
  • 1
  • 6