K
K
Kirill Bazhenov2017-09-26 10:21:52
Java
Kirill Bazhenov, 2017-09-26 10:21:52

How to make if (the user enters something from the keyboard and it will be == change) then output to the console?

import  java.util.Scanner;
public class Main {
    public static void main(String[] args) {
       
Scanner n = new Scanner(System.in);
String[] a = {"Привет", "Пока"}

        if (n == a[0]) {
System.out.println(a[0])
}
else {
System.out.println(a[1])
}

        
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cyril, 2017-10-05
@krog

First, strings cannot be compared using == . Use Objects.equals
In general, probably, something like that should be:

Console console = System.console();
        if (console == null) {
            //do smth
            return;
        }

        String userInput = console.readLine("Пользователь, введи что-нибудь: \n");
        String[] answers = {"Привет", "Пока"};

        if (Strings.isNullOrEmpty(userInput)) {
            //do smth
            return;
        }

        System.out.printf("Пользователь говорит %s\n", userInput);

        if (Objects.equals(answers[0], userInput)) {
            System.out.println(answers[0]);
        } else {
            System.out.println(answers[1]);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question