Results 1 to 2 of 2
Thread: Try catch response
- 03-30-2009, 07:08 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 29
- Rep Power
- 0
Try catch response
I have no errors, but when you enter the number for type of temp you want to convert, it runs fine until where you enter the temp, if I enter ty(not a numerical value) it gives a numerical response, while if I eneter a numerical value, it displays the value and closes without showing the conversions...
i have the try catch in the Set functions..any ideas?
thanks againJava Code:import javax.swing.*; import javax.swing.JOptionPane; public class Main { private double F; double C; double K; int iChoice; public static void main( String[] args ) { Main oTemp = new Main(); oTemp.SetChoices(); } public void SetChoices() { try { String choice = JOptionPane.showInputDialog(null, "Choose temperature input type: \n 0 to exit \n 1 for Fahrenheit \n 2 for Celsius \n 3 for Kelvin"); iChoice = Integer.parseInt(choice); } catch ( NumberFormatException nfe) { SetChoices(); } switch (iChoice) { case 0: if (iChoice == 0) { break; } case 1: if (iChoice == 1) { SetFahrenheit(); //GetCelsius(); //GetKelvin(); break; } case 2: if (iChoice == 2) { SetCelsius(); //GetFahrenheit(); //GetKelvin(); break; } case 3: if (iChoice == 3) { SetKelvin(); //GetFahrenheit(); //GetCelsius(); break; } default: { SetChoices(); break; } } } public void SetFahrenheit() { try { String fahrenheit = JOptionPane.showInputDialog(null, "Please enter the temperature in Fahrenheit" ); JOptionPane.showMessageDialog(null, "You entered the temp " + fahrenheit); F = Double.parseDouble(fahrenheit); } catch ( NumberFormatException nfe) { GetCelsius(); GetKelvin(); } } public void SetCelsius() { try { String celsius = JOptionPane.showInputDialog(null, "Please enter the temperature in Celsius" ); JOptionPane.showMessageDialog(null, "You entered the temp " + celsius); C = Double.parseDouble(celsius); } catch ( NumberFormatException nfe) { GetFahrenheit(); GetCelsius(); } } public void SetKelvin() { try { String kelvin = JOptionPane.showInputDialog(null, "Please enter the temperature in Kelvin" ); JOptionPane.showMessageDialog(null, "You entered the temp " + kelvin); K = Double.parseDouble(kelvin); } catch ( NumberFormatException nfe) { GetFahrenheit(); GetCelsius(); } } public void GetCelsius() { if (iChoice == 1) { double Celsius = (5.0 / 9.0) * (F - 32); JOptionPane.showMessageDialog(null, "The temperature in Celsius is " + Celsius); } else if (iChoice == 3) { double Celsius = K - 273.15; JOptionPane.showMessageDialog(null, "The temperature in Celsius is " + Celsius); } } public void GetFahrenheit() { if (iChoice == 2) { double Fahrenheit = (C * 9/5 + 32); JOptionPane.showMessageDialog(null, "The temperature in Fahrenheit is " + Fahrenheit); } else if (iChoice == 3) { double Fahrenheit = (K * 9/5 - 459.67); JOptionPane.showMessageDialog(null, "The temperature in Fahrenheit is " + Fahrenheit); } } public void GetKelvin() { if (iChoice == 1) { double Kelvin = (F + 459.67) * 5/9; JOptionPane.showMessageDialog(null, "The temperature in Kelvin is " + Kelvin); } else if (iChoice == 2) { double Kelvin = (C + 273.15); JOptionPane.showMessageDialog(null, "The temperature in Kelvin is " + Kelvin); } } };
- 03-30-2009, 08:30 PM #2
the catch blocks in your set methods have a logic bug. think about what you what it to do when you have a number format ex.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Similar Threads
-
how to catch two exceptions in one catch()?
By arnab321 in forum New To JavaReplies: 1Last Post: 11-06-2008, 10:54 AM -
try catch!?
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 01-28-2008, 07:51 PM -
Try Catch
By Renegade85 in forum New To JavaReplies: 4Last Post: 12-03-2007, 04:10 PM -
when to use try...catch
By javaplus in forum New To JavaReplies: 2Last Post: 11-18-2007, 08:52 PM -
Use try and catch
By zoe in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks