im trying to figure out how to get the max and min of the values that are entered if i run this program. I want the user to enter in a series of numbers until they were finished and to terminate it to type -99. I'm having quite a few issues. First i can only get it to go through the while statement if i use -99 as my first number. Once i get through the while statement i dont know how to come up with the min or max of the values. Help please.

import javax.swing.JOptionPane;


public class maxandmin {
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		double number;
		String input;
		int cap = -99;
		
		input = JOptionPane.showInputDialog("Enter the first number");
		number = Integer.parseInt(input);
		
		
		while(number != cap){
			JOptionPane.showInputDialog("Enter the next number");
			
			number = Integer.parseInt(input);
		}
		
		
			
		
		
			
			
			JOptionPane.showInputDialog("The smallest of the numbers [INDENT][INDENT][INDENT][/INDENT][/INDENT][/INDENT]entered was : " +  // + "/n" 
						 /*+*/ "The largest of the numbers [INDENT][INDENT][INDENT][/INDENT][/INDENT][/INDENT]entered was : ")/* + )*/;

		// TODO Auto-generated method stub

	}

}

Recommended Answers

All 7 Replies

sorry about the indenting toward the bottom

How would you solve this problem if you had to write down everything vs remember the numbers you were given? Say you had two pieces of paper, one for largest and one for smallest. I give you a number. What would you do?
You'd compare what I gave you to the current largest and smallest number you'd written down. If the new number was less you'd replace the last smallest with the new smallest.
And same with largest.
Continue until the last number.

i can only get it to go through the while statement if i use -99 as my first number

What happens if you enter 33?
Add a print statement immediately after the while() statement:

System.out.println("number=" + number);

Some tricks are needed to get the first numbers. Use a boolean flag to test for the first number and if it is, set both the largest and smallest to the first number, set the flag to remember that you have done it and go get the next number.

Should we first establish the input channel properly?
(1) In line 20 the "input= " is missing.
The corrected code would be:
input = JOptionPane.showInputDialog("Enter the next number");
(2) lines 15, 16 are deleted.
(3) before comming into the while loop the variable number should be initiated, e.g., as 0.
(4) try to print out each input. Test if the channel is established successfully or nor.
Then you may consider how to get max/min values among the series of input values.


Some tricks are needed to get the first numbers. Use a boolean flag to test for the first number and if it is, set both the largest and smallest to the first number, set the flag to remember that you have done it and go get the next number.

can you give me an example. I dont quite understand what you mean by boolean flag.

boolean firstTime = true:


if(firstTime) {
   firstTime = false:
   .. do the first time stuff here
   continue;  // skip rest of stuff in this loop
}

Should we first establish the input channel properly?
(1) In line 20 the "input= " is missing.
The corrected code would be:
input = JOptionPane.showInputDialog("Enter the next number");
(2) lines 15, 16 are deleted.
(3) before comming into the while loop the variable number should be initiated, e.g., as 0.
(4) try to print out each input. Test if the channel is established successfully or nor.
Then you may consider how to get max/min values among the series of input values.

cant believe i missed that input. Thank you that solved why it was messing up with updating what numbers i was entering.

very helpfull thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.