-1
package kreki;
import java.util.Scanner;
public class Hungry {
    public static void main(String [] args) {
        String input;
        char ans;
        Scanner Hungry = new Scanner(System.in);
        System.out.println("Are You Hungry?");
        input = Hungry.next();
        ans = input.charAt(0);
        if(ans == "Y") {
            System.out.println("Eat");
        }else {
            System.out.println("Starve");
        }
        Hungry.close();
    }
}

I'm getting Type mismatch: cannot convert from char to String. How can I fix it? Thank you.

1

2 Answers 2

4

It can be hard to spot for new in Java/programming overall.

Error is here:

if(ans == "Y") {

Change double quotes to single one

if(ans == 'Y') {
Sign up to request clarification or add additional context in comments.

Comments

1

In the if:

  if(ans == 'Y') {
        System.out.println("Eat");
    }

you just need so change to single quote, because those are used for char symbols in Java. When you're using double quotes, Java is considering it a String, therefore a mismatch of types on comparison.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.