samedi 18 novembre 2017

Beginner here - java IF ELSE used with text

I just started reading about JAVA and I want to make a little program that when using Scanner is I type "yes", "no" or just something random I will get different messages.The problem if with the lines:

 if (LEAVE == "yes") {
        System.out.println("ok, lets go");
     if (LEAVE == "no") {
        System.out.println("you dont have a choice");
} else {
        System.out.println("it's a yes or no question");

I receive the error : Operator "==" cannot be applied to "java.util.scanner", "java.lang.String". I saw on a site that it would be better if I replaced "==" with .equals,but I still get an error..

Help please :S

Code below:

package com.company;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner LEAVE = new Scanner(System.in);

        System.out.println("do you want to answer this test?");
        LEAVE.next();

        System.out.println("first q: would you leave the hotel?");
        LEAVE.next();
        if (LEAVE == "yes") {
            System.out.println("ok, lets go");
        }
        LEAVE.nextLine();
        if (LEAVE == "no") {
            System.out.println("you dont have a choice");
            LEAVE.nextLine();
        } else {
            System.out.println("it's a yes or no question");

        }
    }}

Aucun commentaire:

Enregistrer un commentaire