0

All,
I have worked extensively on my looping and sentinels here. As you can see I have my program working but it is not taking the series. I know I am missing something stupid but I just can't put my finger on it. I have my inupt validation working as our professor likes but it is just not doing the series of numbers. I must have my output, process, and input seperate from one another. Can someone point me in the right direction. I think I have overwhelmed my brain with the parts I have completed. I don't even need exact code snippets either- just a direction would be nice.

import javax.swing.*;
   import javax.swing.JOptionPane;
   import javax.swing.UIManager;
   import javax.swing.plaf.ColorUIResource;
   import java.text.DecimalFormat;
	
    public class LargestandSmallestV1
   {
       public static void main(String[] args)
      { 
      
      
      // ********* Create Objects *********	
      	
      	
         UIManager uim=new UIManager();  															 // Imports UIManager
         uim.put("OptionPane.background",new ColorUIResource(0,0,190));					 // Picks panel ackground color
         uim.put("Panel.background",new ColorUIResource(0,0,190));						 // Picks pane background color
         uim.put("OptionPane.messageForeground",new ColorUIResource(255,255,255));	 // Picks pane message color
         uim.put("Panel.messageForeground",new ColorUIResource(255,255,255));   		 // Picks panel message color
      
      
      
      
      // ********* Declare Const and Init *********

      		
         final int ZERO = 0;	// sets system exit value
      	
         final String WELCOME = "Welcome to the Largest and Smallest Program.\n" +
            							  "Once you would like to exit the series\n" +
            							  "of numbers enter -99 to do so.";  // Welcomes user and gives exit code	
			final String SERIES_REQUEST = "How many numbers would you like to enter?";  // Requests number of entries								  		 
      	
         final String GET_NUMS = "Enter a series of numbers that includes\n" +
            							 "only integers (0-9 or decimal): ";  // 1st request-- series of numbers			 
      	
         final String OUTPUT_1 = "The smallest number is: ";  // 1st part of output text
      	
         final String OUTPUT_2 = "\nThe largest number is: ";  // 2nd part of output text
      	
         final String THANKS = " Thank you for using Largest and Smallest Program";	   // Thank you messge                                     
      
         final String ERROR = "Please input a valid number (0-9 and decimals only)."; // Error message for user
      	
         final String QUIT = "-99"; // holds exit value for user series exit
      
      
      // ********* Declare Vars and Init *********
      
      
         double inputNum;  		  		   // declare var for input numbers
      	
         int counter = 0;					// counter for process
      	
         int i = 0;							// declare and initialize "i" for counting 
      
         String input = ""; 				// takes input from user for conversion
      	
         boolean goodNum = true;			// for holding input validation flag
      	
         double largest = 0; 					// for holding largest integer entered
      	 
         double smallest = 0;					// for holding smallest integer entered
      	
         double seriesLength = 0;
      
      
       // ********* Show Welcome *********

      	
         JOptionPane.showMessageDialog(null, WELCOME,null,JOptionPane.PLAIN_MESSAGE); // Displays Luthers Welcome
      
      
      
      // ********* input USERS SERIES OF MUMBERS *********
      
      
         while (seriesLength <0 || input.equals(""))
         {      
            seriesLength = Double.parseDouble(JOptionPane.showInputDialog(null,SERIES_REQUEST,null,
               											JOptionPane.QUESTION_MESSAGE)); // gets input from user
																      
            input = JOptionPane.showInputDialog(null,GET_NUMS,null,JOptionPane.QUESTION_MESSAGE); // gets input from user
         }	// tests for null string -- ENDS WHILE 
      	
			
      
      
         while (goodNum && counter < seriesLength)
         {
         
         
            for (i = 0; i < input.length(); i++)
            {
            	
               if ((input.charAt(i) !='.') && (input.charAt(i) <'0') || (input.charAt(i) >'9'))
               
               {
                  goodNum = false; 	// set flag to incorrect data type
               }		// End of IF STATEMENTS
            		
               counter++;		// increment to next position in string           		
            			
            }			// end of FOR STATEMENTS
         		
         		
            while (!goodNum)
            {
               input = JOptionPane.showInputDialog(null,ERROR,null,JOptionPane.ERROR_MESSAGE); // gives error          
            
               goodNum = true;	  // reset flag to true
            	            	
               counter = 0;		  // reset counter for string
            	
            	
            
            } 	// end while !goodNum
         } 		// end while goodNum and counter < length
      
      
      
      	
      // ********* process *********
      
         inputNum = Double.parseDouble(input); // changes input string to double
			
			
         if (inputNum > largest)
         {
            largest=inputNum; 
         } 
          
                     
         if (inputNum < smallest)
         {
            smallest=inputNum;    
         }
      
      
      
      // ********* output *********		
      
      
         JOptionPane.showMessageDialog(null,(OUTPUT_1 + smallest  + OUTPUT_2 + largest),null,
            									JOptionPane.INFORMATION_MESSAGE);	// gives user output message
      	
         JOptionPane.showMessageDialog(null, THANKS,null,JOptionPane.PLAIN_MESSAGE);  // shows thank you message	
      	 
      	 
      	 
      	 
         System.exit(ZERO);// ends program
      		
      		
      } 		// End of Main
   	
   	
   }	  // End of Largest and Smallest V1

Edited by astronomical: n/a

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by javaAddict
1

I couldn't understand what your problem is so I tried to run it.
It asked me to enter some numbers, but once I entered the first number, it exited without asking for more.

It is because you call this: input = JOptionPane.showInputDialog(null,GET_NUMS,null,JOptionPane.QUESTION_MESSAGE); only once outside the while loop.
Now since you already make that call once, and you enter the loop with input having value, I would suggest that you put it at the end of the loop:

while (goodNum && counter < seriesLength)
{



input = JOptionPane.showInputDialog(null,GET_NUMS,null,JOptionPane.QUESTION_MESSAGE);
} 		// end while goodNum and counter < length

Also , if you run it now you will see that it always displays the lowest to be 0 and the max the last number entered.
It is because:
> you initialize the lowest with 0
> You have those checks outside the loop.

You need to put these checks inside the loop and initialize those variables with the initial input:

while (seriesLength <0 || input.equals(""))
{      
            seriesLength = Double.parseDouble(JOptionPane.showInputDialog(null,SERIES_REQUEST,null,
               											JOptionPane.QUESTION_MESSAGE)); // gets input from user
																      
            input = JOptionPane.showInputDialog(null,GET_NUMS,null,JOptionPane.QUESTION_MESSAGE); // gets input from user
}

largest=inputNum;
smallest=inputNum;

while (goodNum && counter < seriesLength)
{



input = JOptionPane.showInputDialog(null,GET_NUMS,null,JOptionPane.QUESTION_MESSAGE);
} 		// end while goodNum and counter < length
Votes + Comments
:) lot of effort to help this kid.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.