0

I'm in a rather unique situation based on what I've seen in other's coding. I'm working on a multi-frame GUI for a school project and it has four converters between various units: Mass, Volume, Distance, and Temperature. The problem I run into lies with the Text Listeners for my temperature conversions:

private class TempListener implements ActionListener{ 
    public void actionPerformed (ActionEvent event){ 
        double faren, cels; 
        String text = temperature.getText(); 
        faren = Integer.parseInt(text); 
        cels = (faren-32) * 5/9; 
        rLabelT.setText(Double.toString(cels)); 
    } 
} 

private class Temp1Listener implements ActionListener{ 
    public void actionPerformed (ActionEvent event){ 
        double faren, cels; 
        String text = temperature1.getText(); 
        cels = Integer.parseInt(text); 
        faren = (cels+32) * 9/5; 
        rLabelT1.setText(Double.toString(faren)); 
    } 
} 

The Integer.parseInt method catches a NumberFormatException.java and stops it from completing the calculation. Now, this coding is almost identical to a similar program from my textbook with the only edits being the variable type. What I worry about is that the other frames have fully functional calculations based on the same block:

private class MassListener implements ActionListener{ 
    public void actionPerformed (ActionEvent event){ 
        double pounds, kilograms; 
        String text = mass.getText(); 
        pounds = Integer.parseInt(text); 
        kilograms = pounds/2.2; 
        rLabelM.setText(Double.toString(kilograms)); 
    } 
} 
private class VolumeListener implements ActionListener{ 
    public void actionPerformed (ActionEvent event){ 
        double litres, gallons; 
        String text = volume.getText(); 
        gallons = Integer.parseInt(text); 
        litres = .264 * gallons; 
        rLabelV.setText(Double.toString(litres)); 
    } 
} 
private class DistListener implements ActionListener{ 
    public void actionPerformed (ActionEvent event){ 
        double miles, kilo; 
        String text = distance.getText(); 
        miles = Integer.parseInt(text); 
        kilo = miles * 1.609; 
        rLabelD.setText(Double.toString(kilo)); 
    } 
} 

All of these work with EXACTLY the same coding without any errors. How do I fix this? It's the last issue in my code to address.

The code to open the frame that contains the temp conversions:

   private class ButtonTListener implements ActionListener{ 
    public void actionPerformed (ActionEvent event){ 
        JFrame dialogT = new JFrame ("Temperature"); 
        dialogT.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
        dialogT.setBounds(750,650,400,150); 
        dialogT.setVisible(true); 
        iLabelT = new JLabel ("Enter a Temperature in Farenheit: "); 
        oLabelT = new JLabel ("Temperature in Celsius: "); 
        rLabelT = new JLabel ("--"); 
        temperature = new JTextField (5); 
        temperature.addActionListener(new TempListener()); 
        iLabelT1 = new JLabel ("Enter a Temperature in Celsius: "); 
        oLabelT1 = new JLabel ("Temperature in Farenheit: "); 
        rLabelT1 = new JLabel ("--"); 
        temperature1 = new JTextField (5); 
        temperature.addActionListener(new Temp1Listener()); 
        Temperature1 = new JPanel(); 
        Temperature1.setPreferredSize(new Dimension (400,100)); 
        Temperature1.setLayout(new FlowLayout()); 
        Temperature1.setBackground (Color.yellow); 
        Temperature1.add(iLabelT); 
        Temperature1.add(temperature); 
        Temperature1.add(oLabelT); 
        Temperature1.add(rLabelT); 
        Temperature1.add(iLabelT1); 
        Temperature1.add(temperature1); 
        Temperature1.add(oLabelT1); 
        Temperature1.add(rLabelT1); 
        dialogT.getContentPane().add(Temperature1); 
    } 
} 
  • 1
    What is the text on which `Integer.parseInt` throws a `NumberFormatException`? – rgettman Jan 14 '14 at 16:48
  • 2
    Why `Integer.parse` when the values `mile` and `kilo` are `double`? – Paul Samsotha Jan 14 '14 at 16:53
  • In Java, the integer division `5/9` is `0`, and `9/5` is `1`. Use `double` literals instead. – rgettman Jan 14 '14 at 16:56
  • Try to use `Double` values everywhere. The error probably comes from a constellation like: `Integer.parseInt(Double.toString(1.0));` – Matt Jan 14 '14 at 17:03
  • @rgettman just bad style with no harm in this case, but `faren` and `cells` are already double values, the rest gets coerced. – Matt Jan 14 '14 at 17:04
  • @Matt Even when I convert everything to double in the functions it still throws the error. – user3194855 Jan 14 '14 at 17:18
  • What text are you trying to parse that causes the exception? – Taylor Jan 14 '14 at 17:19
  • @Taylor text from the temperature/temperature1 JTextFields – user3194855 Jan 14 '14 at 17:23
  • Lord. Complete the following: I tried to parse the string `________` as an integer but it threw a number format exception. – Taylor Jan 14 '14 at 17:24
  • @Taylor Sorry if I'm being irritating. I'm just frustrated and confused. I tried to parse the String 'text' as an integer. 'text' references 'temperature'. When I parse the String 'text' as a Double value it still throws the error. – user3194855 Jan 14 '14 at 17:27
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jan 14 '14 at 18:08
  • @AndrewThompson I read the article and thanks for that. I agree with it because I tried the JInternalFrame and JDialog methods and just got more and more frustrated until I just came back to the JFrame method. That's not my issue though, the frames display perfectly fine – user3194855 Jan 15 '14 at 01:43
  • Can you post the stacktrace of your excepetion? (So that we see which line exactly is causing the error) – Matt Jan 15 '14 at 07:45

1 Answers1

2

You declare double values, and then try to parse them with Integer. An integer is, by any definition, a number with no decimal place. So when Java encounters any decimal place in your input, it will fail. Instead, you should be trying to parse them using Double.

For example:

    double d1, d2;
    try
    {
        System.out.println("Parsing d1...");
        d1 = Integer.parseInt("5.4");    // using Integer to parse a double fails
        System.out.println("d1: " + d1);
    }
    catch (NumberFormatException e1)
    {
        System.err.println("exception parsing 5.4 as an integer");
        e1.printStackTrace();
    }

    try
    {
        System.out.println("Parsing d2...");
        d2 = Double.parseDouble("5.4");    // using Double to parse a double succeeds
        System.out.println("d2: " + d2);
    }
    catch (NumberFormatException e2)
    {
        System.err.println("exception parsing 5.4 as a double");
        e2.printStackTrace();
    };

Output:

Parsing d1...
exception parsing 5.4 as an integer
java.lang.NumberFormatException: For input string: "5.4"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:458)
    at java.lang.Integer.parseInt(Integer.java:499)
    at pkg.Main.main(Main.java:40)
Parsing d2...
d2: 5.4
mdl
  • 406
  • 2
  • 9
  • Thanks for the advice. I changed the parse to double as well in accordance to your try-catch example but it still decided not to work and pull an exception. – user3194855 Jan 15 '14 at 01:41
  • My bad, I got distracted with life. I solved the problem, it was a variable issue in the listener – user3194855 Feb 09 '14 at 22:32