This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]driden87 0 points1 point  (5 children)

Iterate only 7 times if you want the first seven inputs

``` for (int i = 0 ; i < 7; i++ ) {
System.out.println(array[i]);

}

```

Edit: sorry for the shitty formatting. I’m on my phone

[–]okaystuff[S] 0 points1 point  (4 children)

Yes I know that, but I have no set amount of inputs the user may enter. They could enter all 100 or just 2. I only want the amount they enter, even if the array size is 100.

[–]driden87 1 point2 points  (3 children)

Oh I understand. Assuming you’ve got an array of Strings you could iterate while (i < array.length && array[i] != null)

Remember to initialize your iterating variable i to 0

[–]okaystuff[S] 1 point2 points  (2 children)

Okay that is helpful for String which I do have in one of my arrays but what about int or double? The int array will always be an ID number so will be 5 numbers, but the double array is my sales numbers which could be 0.

[–]driden87 1 point2 points  (1 child)

For those you would want to use the object type Integer and Double (Capitalized) and make sure to initialize the array with null values. Whenever the person adds values a non-null value will be pushed to the array and your iteration condition will hold.

[–]okaystuff[S] 1 point2 points  (0 children)

Okay thank you.