File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Soo close Part II Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Soo close Part II" Watch "Soo close Part II" New topic

Soo close Part II

Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
Hi out there,
If you had a chance to read my last message (soo close) then you know what my problem was. Well I got it to break when 0 was entered but now my return is always 0. I know I'm suppose to make the smallest integer exclude 0 but I've tried everything I can think of. I can't find any examples in my book thus far telling me how to do that. So again can anyone help me? My code to return the smallest integer is as follows:
/**Find the smallest integer*/
int small = integers[0];
for (int x = 1; x < integers.length; x++) {
if (small > integers[x])
small = integers[x];

Thanks so much.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

If I understand, based on your so close post, the problem you are trying to solve is that the user enters a series of integers; they can keep entering integers until they either:
1) have entered 30 integers
2) they enter '0' to indicate they are done entering integers
As soon as one of these conditions is met, the program determines the lowest integer and outputs it. However, in the second case, the '0' should not be part of the array of integers; it acts only as a trigger. So in the scenario:
5 4 9 0 � the program should output 4, not 0
And the issue you are having now is that your program does output the zero.
Assuming that is a correct summary, your program is outputting zero because you are not allowing for (i.e. dealing with) the initialization values of the array. When you create the array, it is initializing all the ints in the array to zero. Try the following experiment to see what I mean:

Notice it prints out: 99 0 0 0 0 0 0 0 0 0 even though I only put the value 99 in.
So in your code, you have all those zeros in your array when you break out of the inout loop before filling the array with inputted values. So when you check for the smallest value in this scenario, you need to stop checking when you hit the last inputted value, otherwise all those zeros are taken into consideration.
So, when you break out of the loop that gets your input values, you need to store the index value of the last inputted character in some attribute, such as lastIndex. Then in your "Find smallest integer" block of code, you need to stop either when you reach that lastIndex value, or when you reach the end of the array.
Be sure to consider all possible extraneous situations: for example, what happens if the first (and thus only) value the user inputs is zero � make sure such a scenario does not crash your program. Also make sure the user can still enter all 30 values without any problems.
Give that a try and if you are still having problems, post your code and someone can help you out; but it will help you learn better if you take the next step -- of determining the code specifics -- on your own.
Good Luck.
I agree. Here's the link:
subject: Soo close Part II
jQuery in Action, 3rd edition