Results 1 to 2 of 2
  1. #1
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default 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?

    Java 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);
                }
            }
        };
    thanks again

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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

  1. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 11:54 AM
  2. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 08:51 PM
  3. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 05:10 PM
  4. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 09:52 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •