• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Ready to give up!

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I've been racking my brains all night with this problem as you can see in my last couple posts. I'm ready to give up. I don't know what else to do. My problem is that I have to get the user to input an array of integers (up to 30) and to break out of the loop before 30 they press 0 to get the result which is the smallest integer entered. I've got everything figured out up until the result. Now that I have my program breaking at 0 it always returns the integer at 0. I understand that I have to store the last index value other than 0 or exclude 0 from returning the smallest integer, but I just can't figure it out. I'm really new to Java - only 5 chapters in my book. So I feel this is over my head as I can't seem to find any examples of it or anything. Can anyone help me, I'm at the end of my rope. My program is as follows:
import javax.swing.JOptionPane;

public class Array5_3 {
public static void main(String [] args) {

int[] integers = new int[30];
for (int x = 0; x < integers.length; x++){

String input = JOptionPane.showInputDialog(null,
"Please enter a number, \nto return the smallest number enter 0.",
"TME 2, Programming Exercise 5.3", JOptionPane.QUESTION_MESSAGE);
integers[x] = Integer.parseInt(input);
if(integers[x] == 0){
break;
}

}

int small = integers[0];
for (int x = 0; x < integers.length; x++) {
if (small > integers[x])
small = integers[x];
}

JOptionPane.showMessageDialog(null, "The smallest integer inputed was "+ small,
"TME 2, Programming Exercise 5.3", JOptionPane.PLAIN_MESSAGE);

System.exit(0);

Stacey
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, well, the quick fix is indeed to just store the last index at which you saved a number, then use that as the upper limit of the second loop. First, move the declaration of the first "x" up to before the loop:

Then use a different counter and loop limit for the second loop:

Now, this should work, but there may be some problems remaining for you to track down. Does the program work if you input 0 numbers? If you input 30 numbers?
[ January 21, 2004: Message edited by: Ernest Friedman-Hill ]
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest (& Stacey),
It looks like you have a small error in your suggested loop:

When you copied and pasted, you forgot to change the index value being checked from x to i. Here the same array item (integers[x]) is being compared in each iteration of the loop; and it is zero.
It needs to be changed to:

Regards,
Mark
[ January 22, 2004: Message edited by: Mark Vender ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good catch! Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic