Sentinel-controlled while loop processing issue

I am to create a program allowing the user to enter in integers, the program will display all integers entered and the maximum and minimum of those integers. The user ends the program by entering -999.

Problem: When the user enters -999 it is recorded as the minimum integers they entered when it should just stop the loop and display the output without itself being displayed. What do I have wrong in my code? Any help you can give me will be very appreciated, thank you.

import javax.swing.JOptionPane;

public class MaxMinOfIntegers

{

public static void main ( String [] args )

{

//preparations

String outputMessage, instructions, openingMessage, listOfNumbers = " ";

String closingMessage;

int number, max = 0, min = 0, numberOfIntegers = 0;

openingMessage = "This program allows a user to enter in numbers, it will keep " +

"track of numbers entered along with the maximum and minimum. \n\n";

instructions= "Please enter a number or enter -999 to quit and view the results. \n";

closingMessage = "End of program, have a nice day. ";

//input

JOptionPane.showMessageDialog ( null, openingMessage );

number = Integer.parseInt ( JOptionPane.showInputDialog ( null, instructions ) );

//processing

while ( number != -999 )

{

number = Integer.parseInt ( JOptionPane.showInputDialog ( null, instructions ) );

listOfNumbers += number + " " ;

if ( number > max )

max = number;

if ( number < min )

min = number;

}

//output

if ( numberOfIntegers == 0 )

outputMessage = "End of program, thank you.";

else

outputMessage = "The numbers you have entered are " + listOfNumbers +

", the maximum number is " + max +

" and the minimum number is " + min + ". \n End of program, thank you.";

JOptionPane.showMessageDialog ( null, outputMessage );

System.exit (0);

//end main method

}

//end class

}

[2042 byte] By [Clipsea] at [2007-11-27 10:12:03]
# 1

You can't do that with a 'Sentinel-controlled while loop', if that's your assignment. You have to put another test and a break (or continue?) inside the loop.

ejpa at 2007-7-28 15:17:48 > top of Java-index,Java Essentials,New To Java...
# 2

You can change your code so that you have:

while (true) {

// read a number

if (number == -999) {

break;

}

//do your calculations here

kajbja at 2007-7-28 15:17:48 > top of Java-index,Java Essentials,New To Java...
# 3

For the most part is was just in the structuring of the code, how things were processed and when they were processed. The following is the reworked code in full working order. Thanks for the two replies however they weren't very helpful. It may have taken me 4 hours to work this but at least I completed it. Thanks again.

import javax.swing.JOptionPane;

public class MaxMinOfIntegers

{

public static void main ( String [] args )

{

//preparations

String outputMessage, instructions, openingMessage, listOfNumbers = " ", integer;

String closingMessage;

int number, max = 0, min = 0, numberOfIntegers = 0;

final int SENTINEL = -999;

openingMessage = "This program allows a user to enter in numbers, it will keep " +

"track of numbers entered along with the maximum and minimum. \n\n";

instructions= "Please enter a number or enter -999 to quit and view the results. \n";

closingMessage = "End of program, have a nice day. ";

//input

JOptionPane.showMessageDialog ( null, openingMessage );

integer = ( JOptionPane.showInputDialog ( null, instructions ) );

number = Integer.parseInt (integer );

min= Integer.parseInt ( integer );

max= Integer.parseInt ( integer );

//processing

while ( number != SENTINEL )

{

listOfNumbers += number + " " ;

if ( number > max )

max = number;

if ( number < min )

min = number;

numberOfIntegers++ ;

number = Integer.parseInt ( JOptionPane.showInputDialog ( null, instructions ) );

} //end while loop

//output

if ( numberOfIntegers == 0 )

outputMessage = "End of program, thank you.";

else

outputMessage = "The numbers you have entered are " + listOfNumbers + ", the maximum number is " + max +

" and the minimum number is " + min + ". \n End of program, thank you.";

JOptionPane.showMessageDialog ( null, outputMessage );

//terminate GUI

System.exit (0);

//end main method

}

//end class

}

Clipsea at 2007-7-28 15:17:48 > top of Java-index,Java Essentials,New To Java...