I am trying to use a while condition where if a user inputs a string with the first character as number 1, the loop should end. However, in my case the loop never ends. What could I be doing wrong?
public static void main(String[] args) {
ArrayList<Integer> instructions = new ArrayList<Integer>();
Scanner keyboard = new Scanner(System.in);
String input = "";
String termIns = input.substring(0);
// int termInsInt= Integer.parseInt(termIns);
do {
input = keyboard.nextLine();
int inputInt = Integer.parseInt(input);
instructions.add(inputInt);
//String termIns = input.substring(0);
} while(!termIns.equals("1"));
In addition, what would display the list of all elements in the ArrayList?
You need to update
termIns
with the user input in each iteration of loop:Also
substring(0)
will not help you asYou can use startsWith method instead directly on input as mentioned here