I am trying to understand gathering user input and looping until conditions. I want to loop a scanner until user inputs 0, however, I need each inputted integer to be stored so it can be accessed for later use. The hard part is, I can't use an array.
-
Which language are you using? – user247702 Sep 23 '13 at 22:40
-
Is this in Java? Also if you can't use an array...what can you use? – William Gaul Sep 23 '13 at 22:40
-
Yes, this is Java sorry – Ryan Sep 23 '13 at 22:43
-
Are you allowed to use `List` if you can't use arrays? – RandomQuestion Sep 23 '13 at 22:43
-
The prof. didn't go over lists, so I doubt it. – Ryan Sep 23 '13 at 22:44
-
2Well, arrays and lists are basic data structures for storing things. I can't think of anything else where you can store for future usages. – RandomQuestion Sep 23 '13 at 22:46
-
use a string and your own seperator. Troll the condition of not using arrays – Osama Javed Sep 23 '13 at 22:47
-
How would I go about doing this if I used an array? – Ryan Sep 23 '13 at 22:47
-
Since you don't know the number of elements in user input, you can't use simple `Array` because to initialize arrays, you need to know the initial size. Easiest approach would be to use `ArrayList` which uses `Array` in it's backend. Refer to my answer. – RandomQuestion Sep 23 '13 at 22:57
-
Use an ArrayList, Stack, Queue, or Set. See my answer below, you have many options. – ElliotSchmelliot Sep 23 '13 at 23:00
3 Answers
simply you can do something like
List mylist = new ArrayList(); //in java. other wise you can create array[size]
int input = 1;
while(input!=0)
{
/* input number from user here */
if(input!=0)
mylist.add(input);
}

- 2,884
- 3
- 28
- 47
Here is an easy way to loop user input until 0
is entered.
Scanner console = new Scanner(System.in);
boolean loop = true;
String input;
while(loop) {
input = console.nextLine();
//Add input to a data structure
if (input.equals("0")) {
loop = false;
}
}
As far as adding the user input to a data structure, you said you can't use an Array. How about a List or a Set. Even a Stack or a Queue would work. Have you looked at using any of these data structures?
Here is a basic example using a List:
List<String> aList = new ArrayList<String>();
aList.add(input);
And this is how you might use a Stack:
Stack<String> stk = new Stack<String>();
stk.push(input);
Perhaps the most efficient way would be to use a HashSet:
Set<String> set = new HashSet<String>();
set.add(input);

- 7,322
- 4
- 41
- 64
Using arrays here would be little tricky since you don't know the numbe of elements user is going to enter. You can always write the code to create a new array with bigger capacity once user has exhaused initial capacity and copy over existing input elements but using List
would be much easier.
Scanner scanner = new Scanner(System.in);
List<Integer> input = new ArrayList<>();
int nextInt = Integer.MIN_VALUE;
while((nextInt = scanner.nextInt()) != 0){
input.add(nextInt);
}
See this question if you really want to use arrays. Answer explains on creating new arrays and copying over elements. Java dynamic array sizes?

- 1
- 1

- 6,778
- 17
- 61
- 97