I was wondering if it was possible to do a loop which continuously adds the integers that the user specifies into the array list and only stops asking the user for inputs once they input something other than an integer.
Here's what I have so far of the method, I just can't think of a way to do what I've mentioned above.
Example would be like:
Enter a number: 1
[1]
Enter a number: 2
[1, 2]
Enter a number: -1
[1, 2 , -1]
Enter a number: abc
(program should end)
Here's what I have so far of the method, I just can't think of a way to do what I've mentioned above.
Code:
public static void do_enter()
{
Scanner scan = new Scanner(System.in);
ArrayList numbers = new ArrayList();
System.out.print("Enter a number: ");
numbers.add(scan.nextInt());
System.out.println(numbers);
}
Enter a number: 1
[1]
Enter a number: 2
[1, 2]
Enter a number: -1
[1, 2 , -1]
Enter a number: abc
(program should end)
Comment