mardi 26 décembre 2017

Java Strings to Booleans

import java.util.Scanner;

public class ChatBot {

    public static Scanner user_input;

    public static void main(String[] args) {

        user_input = new Scanner(System.in);

        String first_name;
        System.out.println("First name please?");
        first_name = user_input.next();

        String last_name;
        System.out.println("Last name please?");
        last_name = user_input.next();

        String full_name;
        full_name = first_name +" " + last_name;

        System.out.println("Hello, " + full_name);

        String greeting;
        greeting = "How are you doing?";

        System.out.println(greeting);

        String first_convers;
        // What do I add here to make an if and else statement?
        first_convers = user_input.next();
    }

}

I am trying to create an if-else statement in the commented area so that if the user inputs "good," then something is printed out and if the user inputs "bad," then something else is printed out. I am using Eclipse Java editor, which is saying:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from String to boolean

at ChatBot.main(ChatBot.java:30)

So basically I can't convert a string to a Boolean. Could you please help me in doing this? Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire