My code is supposed to keep asking the user to type in a string (where the user types in an integer) and convert it to an integer until the user types in -1 which will then make the program break, but I am confused on how to do that?
import java.util.Scanner;
public class StringConverter {
public static void main(String[] args) {
boolean flagBoolean = true;
while (flagBoolean) {
Scanner keyBoard = new Scanner(System.in);
System.out.println("Your number:");
String input = keyBoard.next();
if (input == -1) {
break;
}
int result = 0;
for (int i = 0; i < input.length(); i++) {
result = result * 10 + input.charAt(i) - '0';
}
System.out.println("Converted to:" + result);
}
}
}
My problem lies in if (input == -1), since input is a String, which will prevent it from compiling.