1

以下小程序在启动时显示为一团糟,但更大的问题是当按下按钮进行计算时,异常会飞。我不确定问题可能是什么。我很确定我的循环是正确的。

这是异常列表,然后是实际代码:

Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:992)
    at java.lang.Double.parseDouble(Double.java:510)
    at bacCalcApplet$ButtonListener.actionPerformed(bacCalcApplet.java:164)
    at java.awt.Button.processActionEvent(Button.java:392)
    at java.awt.Button.processEvent(Button.java:360)
    at java.awt.Component.dispatchEventImpl(Component.java:4737)
    at java.awt.Component.dispatchEvent(Component.java:4565)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:643)
    at java.awt.EventQueue$1.run(EventQueue.java:641)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:657)
    at java.awt.EventQueue$2.run(EventQueue.java:655)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

代码

//@author Noah Kline

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class bacCalcApplet extends Applet
{

/**
 * Calculates the BAC
 */
// Create Panel Properties
private static final long serialVersionUID = -8477155615510893945L;
private Panel fPanel;
private Panel answerPanel;
private Panel dPanel;
private Panel wPanel;
private Panel hPanel;
private Panel buttonPanel;
private TextField genderDisplay;
private TextField drinksCount;
private TextField weightDisplay;
private TextField hoursDisplay;
private TextField bacDisplay;

public void init()
{
    // Initialize panels
    buildGenderpanel();
    buildDrinkpanel();
    buildWeightpanel();
    buildHourspanel();
    buildanswerPanel();
    buildButtonPanel();

    // panel layout design in 1 applet
    setLayout(new GridLayout(8, 1));

    // adding panel properties to layout
    add(fPanel);
    add(dPanel);
    add(wPanel);
    add(answerPanel);
    add(buttonPanel);

}

private void buildGenderpanel()
{

    fPanel = new Panel();

    Label message1 = new Label("Enter Gender:");

    genderDisplay = new TextField(10);

    fPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

    fPanel.add(message1);
    fPanel.add(genderDisplay);

}

private void buildDrinkpanel()
{

    dPanel = new Panel();

    Label message2 = new Label("Enter # of Drinks:");

    drinksCount = new TextField(10);

    fPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

    fPanel.add(message2);
    fPanel.add(drinksCount);

}

private void buildWeightpanel()
{

    wPanel = new Panel();

    Label message3 = new Label("Enter Your Weight:");

    weightDisplay = new TextField(10);

    wPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

    wPanel.add(message3);
    wPanel.add(weightDisplay);

}

private void buildHourspanel()
{

    hPanel = new Panel();

    Label message4 = new Label("Enter Hours since last drink: ");

    hoursDisplay = new TextField(10);

    hPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

    hPanel.add(message4);
    hPanel.add(hoursDisplay);

}

private void buildanswerPanel()
{

    answerPanel = new Panel();

    Label message5 = new Label("Answer: ");

    bacDisplay = new TextField(10);

    // answerPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

    bacDisplay.setEditable(false);

    answerPanel.add(message5);
    answerPanel.add(bacDisplay);

}

private void buildButtonPanel()
{

    buttonPanel = new Panel();

    Button convButton = new Button("Calculate");

    convButton.addActionListener(new ButtonListener());

    buttonPanel.add(convButton);
}

// Listener
private class ButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {

        double gender;
        double drinks;
        double weight;
        double hours;
        double bac;

        DecimalFormat fmt = new DecimalFormat("0.0000");

        gender = Double.parseDouble(genderDisplay.getText());

        drinks = Double.parseDouble(drinksCount.getText());

        weight = Double.parseDouble(weightDisplay.getText());

        hours = Double.parseDouble(hoursDisplay.getText());

        bac = Double.parseDouble(bacDisplay.getText());

        if (gender == 1)
        {
            bac = ((drinks * .06 * 100 * 1.055) / (weight * .68))
                    - (0.015 * hours);

            bacDisplay.setText(fmt.format(bac));

        }

        else if (gender == 2)
        {

            bac = ((drinks * .06 * 100 * 1.055) / (weight * .55))
                    - (0.015 * hours);

            bacDisplay.setText(fmt.format(bac));

        }

    }
}

}
4

1 回答 1

0

这是最终产品,减去一个小程序(使用 jQuery 构建): http ://bac.klinetel.com/

于 2012-06-28T02:48:26.653 回答