N
N
NosferatuZodd2019-07-09 20:51:45
Java
NosferatuZodd, 2019-07-09 20:51:45

Why doesn't Java understand the condition?

System.out.println("Все верно?(y/n)");
Scanner answ = new Scanner(System.in);

if (answ.nextLine() == "y")
   System.out.println("Ok");
if (answ.nextLine() == "n")
    System.out.println("Try again");
else
    System.out.println("Неверный ввод(y/n)");

Outputs:
Is everything correct? (y/n)
y
// I write 'y'
y // But it tries to read the input again, I enter 'y' again
Invalid input (y/n) // And in the end it outputs this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2019-07-09
@dolphin23

Because you need to read about the equals() method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question