I'm writing a program in java that requires the user to input a series of numbers, for example 1 2 3 4 .. until the user decides to stop and press enter. Normally what I would do is something like this
Scanner scan = new Scanner (System.in); int[] array = new int[i]; for (int x = 0; x < i; ++x) { array[i] = scan.nextInt(); } The problem is, in most cases i would have a set value. For example, I would ask how many numbers are going to be entered or something like that and that would be stored into i. In this case, i doesn't have a set value to begin with. The user should have the ability to enter as many numbers as he/she wants. Any suggestions?
I tried this and I know the problem with it is that it will never exit the loop but maybe I'm on the right track or something so I'll post it here anyway.
for (int i = 0; i < x; i++, x++) { numbers[i] = scan.nextInt(); numbers = new double[x]; }
ArrayListinstead of an array. It resizes itself as needed.