• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Soo close Part II

Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!