TheRedRipper
TheRedRipper

Reputation: 1

Java incompatible type boolean and char

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?

Upvotes: 0

Views: 677

Answers (2)

knbr
knbr

Reputation: 21

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

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201439

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))

Upvotes: 6

Related Questions