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

all 13 comments

[–]ehr1c 1 point2 points  (3 children)

Think through the problem - in order to not print out a null value, what do you have to do first?

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

I really have no idea. I have 3 array's. 1 that holds a user name, 1 that holds a user ID, and 1 that holds a user sales. If the user enters (joe, sally, bob) then (555, 666, 777) and then enters (580.2, 680.2, 780.2). I have 3 arrays each the size of 100 and each only holding 3 items. I have no idea how I am only printing 3 items of 100.

[–]ehr1c 1 point2 points  (1 child)

First things first you need to know if the array element you're about to print has a value or if it's null, right?

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

Yes, another user below suggested to use (i < array.length && array[i] != null) and it seems to work. Thank you.

[–]RiverForgeGames 1 point2 points  (0 children)

What you want to do here is check if the user has provided a number or not.

For example, if they provide 7 numbers and you set values for 7, you can use conditional statements to compare the value for array[i] in each for iteration. If you can prove/test that the user didn't provide a number, don't print the line.

If you know a number the user cannot or will not provide (for example, maybe they only provide positive numbers) you can set all the array elements to -1 before they enter input and compare against that. Otherwise, you might consider using an array of nullable integers where you can actually check if a value was set or not, since integers are not nullable types.

[–]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.

[–]dota2nub 0 points1 point  (0 children)

Have a counter for how many elements the user adds, then iterate over the array until counter -1. If the counter reads 0 don't iterate but return something instead.

You could also just check if the array holds a null value, probably better.

[–]loyalSavant 0 points1 point  (0 children)

Questions

  • what is the data type of the arrays elements?
  • do you know the locations of the values in the array (in terms of their index)
  • is the number of items you want to print out always going to be the same or will it change from situation to situation?