I've got a problem with looping a scanner, wich should return EVERY string given as an input unless String != "end"
. Here is what I've done so far
private static String fetchString() {
Scanner scanner = new Scanner(System.in);
String stringElement = "";
System.out.println("Write a string");
while(scanner.hasNextLine()) {
stringElement = scanner.next();
if(stringElement == "end") {
break;
}
}
return stringElement;
}
result:
Write a string
abc
abc
abc
end
end
Loop, somehow, doesn't understand if(stringElement == "end")
, it still wants new word. I can't get it. Where am I making a mistake?