Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JOptionPane and do-while loops

843789Sep 15 2010 — edited Sep 16 2010
First post on the forums so let me preface this with saying this is my first java attempt at anything. I have some minor programming background in C++. In short I have this program that is computing wind chill, but I have to validate two inputs from the user and then basically a repeat calculations via a YES_NO_OPTION. Now where I am running into difficulties is the do while loops.

1) the first to validate that the temp is between -58 and 41
2) the second to validate that mph is > 2
3) is if the user wants to calculate more

I for the life of me cannot seem to figure out how to get the validate loops (item 1, 2) to even run without errors...so I took them out, but I would like to have this in there...obviously. I am just not used to parsing strings and such so I am unsure how to take this into account for my do-while loop. I can get the error message to generate, but cant loop back to have the usere enter number again. My final issue is with the repeat via JOptionPane.YES_NO_OPTION. I have the do while already in my program and it runs, although it runs the same message when I click yes or no. If I click on yes it repeats, but shows my closing message. If i choose no then it displays my closing message and no repeat. It just seemed strange to me. If anyone can shed a bit of insight for me I would greatly appreciate it.
//IMPORTS
import javax.swing.*;
import java.util.*;
import java.util.Scanner;
import java.text.*;

public class WindChillQ1
{
	public static void main ( String [] arg)
	{
			 	

	int retry;

do{
//USER INPUT TEMPRATURE
		
String tempS = JOptionPane.showInputDialog(null, "Enter the temprature: -58F to 41F", 
			"Outside temprature", JOptionPane.QUESTION_MESSAGE);

 int temp = Integer.parseInt(tempS);//PARSE TO INTEGER

	if (temp < -58 || temp > 41)//CHECK IF IN RANGE & VALIDATE
	{
	JOptionPane.showMessageDialog(null, "Temprature must be between -58F and 41F", 
		"Out of Range", JOptionPane.ERROR_MESSAGE);
	}
		

//USER INPUT WIND SPEED		

 String mphS = JOptionPane.showInputDialog(null, "Enter the wind speed in mph:  > or = 2 mph", 
			"Wind Speed", JOptionPane.QUESTION_MESSAGE);
																			
     int mph = Integer.parseInt(mphS);//PARSE TO INTEGER

	if (mph < 2)//CHECK IF IN RANGE & VALIDATE
	{
	JOptionPane.showMessageDialog(null, "Wind speed must be equal or greater than 2 mph", 
		"Out of Range", JOptionPane.ERROR_MESSAGE);
	}
			

			
//CALCULATES THE TOTAL WIND CHILL(twc)			
double twc = (35.74 + (0.6215 *temp) - (35.75 * Math.pow(mph,0.16)) + 
(0.4275 * (temp) * Math.pow(mph, 0.16)));

//FORMAT AND DISPLAY OUTPUT
 NumberFormat ftwc = new DecimalFormat("#0.00");
String output = "With a temprature of:  " + temp + "F" 
	+ "\nAnd wind speeds of:  " + mph + "mph" 
	+"\nThe wind chill temprature is:  " + ftwc.format(twc) + "F";
	 JOptionPane.showMessageDialog(null, output);
			
			
		 
//WOULD THE USER LIKE TO DO ANOTHER CALCULATION		 
 retry = JOptionPane.showConfirmDialog(null, "Compute another wind chill temprature?", 
	"Calculate more", JOptionPane.YES_NO_OPTION);

 if (retry == 1);
 {
JOptionPane.showMessageDialog(null, "Thanks for using this program!", 
     "Goodbye", JOptionPane.INFORMATION_MESSAGE);
 }
		
}while (retry == 0);
	
	
	}    
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 14 2010
Added on Sep 15 2010
14 comments
7,617 views