tags:

views:

32

answers:

2

Been trying to change my code all around to catch the exceptions but can't figure out how to go about it with this one. I run into double instantiation of the JOptionPane from time to time too depending on where I put it.

import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class JCalculateWeight extends JFrame {

    Font font = new Font("Arial", Font.BOLD, 14);
    JFrame frame;
    String name = JOptionPane.showInputDialog(frame, "What's your name?");
    String weight = JOptionPane.showInputDialog(frame, "What's your weight (lbs)");

    @Override
    public void paint(Graphics brush) {
        super.paint(brush);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        double lbs = Double.parseDouble(weight);
        double oz = lbs * 16;
        double kilo = lbs / 2.204623;
        double metricton = lbs / 2204.623;
        brush.setFont(font);
        brush.drawString(name, 55, 50);
        brush.drawString(String.valueOf(lbs), 55, 150);
        brush.drawString(String.valueOf(oz), 55, 170);
        brush.drawString(String.valueOf(kilo), 55, 190);
        brush.drawString(String.valueOf(metricton), 55, 210);
    }

    public static void main(String[] args) {
        JCalculateWeight frame = new JCalculateWeight();
        frame.setSize(500, 450);
        frame.setVisible(true);
    }
}
A: 

You're talking about NumberFormatException whenever the enduser enters invalid weight, isn't it? And you want to redisplay the dialog whenever the user input is invalid?

One of the ways is to do that in a loop as long as the weight is zero. Do it inside the main() method after the frame is created. Here are the changes.

// ...
String name;
double weight;

@Override
public void paint(Graphics brush) {
    // ...
    double lbs = weight; // "weight" can also just be renamed to "lbs", then you can remove this line.
    // ...
}

public static void main(String[] args) {
    JCalculateWeight frame = new JCalculateWeight();
    frame.name = JOptionPane.showInputDialog(frame, "What's your name?");
    while (frame.weight == 0) {
        try {
            frame.weight = Double.parseDouble(JOptionPane.showInputDialog(frame, "What's your weight (lbs)"));
        } catch (NumberFormatException e) {
            // Maybe log it? Maybe change the question message?
        }
    }
    // ...
}
BalusC
A: 

You are very new to swing, I think.

I don't know what Exceptions you are talking about, but I should do it this way:

public WeightCalculatorFrame extends JComponent
{
    private String name;
    private double lbs;

    public WeightCalculatorFrame()
    {
        name = JOptionPane.showInputDialog("What is your name?");
        try
        {
            lbs = Double.parseDouble(JOptionPane.showInputDialog("What is your weight in lbs"));
        } catch (NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Invalid decimal format!\nWill now exit");
            System.exit(-1);

        JFrame frame = new JFrame("Weight Calculator");
        frame.setPreferredSize(new Dimension(300, 400));
        frame.add(this);
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        g.setFont(yourFont);
        g.drawString("Your Name: " + name, x, y);
        double kilo = lbs / ....;
        double ... = lbs / ....;
        // Here all your calculations...
        g.drawString("LBS: " + lbs, x, y);
        g.drawString("Kilo: " + kilo, x, y);
    }
}

Initialize it with this:

new WeightCalculatorFrame();
Martijn Courteaux