This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Ready to give up! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Ready to give up!" Watch "Ready to give up!" New topic
Author

Ready to give up!

Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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 ]

[Jess in Action][AskingGoodQuestions]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Good catch! Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ready to give up!
 
Similar Threads
Sooo Close (Arrays & exit loop w/ 0 )
Soo close Part II
Arrays & loops
Simple Java problem
Help on testprogram and subclass please