Ask coding questions

← Back to all posts
Scanner doesn't detect a string
adsarebbbad

At line 7 when I input e, the program exits. Why?

Answered by emilyDoesPascal [earned 5 cycles]
View Answer
Voters
adsarebbbad
Comments
hotnewtop
emilyDoesPascal

using == in java does not test if the strings are equal. Java instead check if they are they same object. You should rather use .equals(), so line 8 would be

if (test.equals("e")) {

However, keep in mind that .equals() will not work on null strings

adsarebbbad