0

我做了一个简单的Java转换器,它将KM转换为M,将摄氏度转换为华氏度,将KG转换为G。转换公里<->米和公斤<->克似乎没问题。但是当我输入温度值时,我总是会收到这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:992)
at java.lang.Double.parseDouble(Double.java:510)
at converter.Converter$2.actionPerformed(Converter.java:73)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6297)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6062)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4660)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
at java.awt.Container.dispatchEventImpl(Container.java:2083)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:668)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:627)
at java.awt.EventQueue$2.run(EventQueue.java:625)
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$3.run(EventQueue.java:641)
at java.awt.EventQueue$3.run(EventQueue.java:639)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:638)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

*很抱歉发布所有内容,但我无法理解为什么会出现此错误。

这是我的代码:

package converter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Converter {
    public static double result;
    public static String indicator;
    public static void main(String[] args) {

    JFrame converter = new JFrame();
    Container content = converter.getContentPane();

    converter.setLayout(new GridLayout (3,3));

    final String[] convertionChoice = {" ", "Kilometer<->Meter", "Celcius<->Fahrenheit", "Kilograms<->Grams"};
    JComboBox convertionList = new JComboBox(convertionChoice);


    final JLabel value1 = new JLabel("---", SwingConstants.LEFT);
    final JLabel value2 = new JLabel("---", SwingConstants.LEFT);
    JLabel blank1 = new JLabel(" ");
    JLabel blank2 = new JLabel(" ");

    final JTextField one = new JTextField();
    final JTextField two = new JTextField();

    JButton convert = new JButton("Convert");
    JButton exit = new JButton("Exit");
    JButton clear = new JButton("Clear All");

     convertionList.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            String currentComboSelection = (String)cb.getSelectedItem();
                 if (currentComboSelection.equals(convertionChoice[1])) {          
                        value1.setText("Kilometer");
                        value2.setText("Meter");
                        indicator = "K/M"; }
                 if (currentComboSelection.equals(convertionChoice[2])) {          
                        value1.setText("Celcius");
                        value2.setText("Fahrenheit");
                        indicator = "C/F"; }
                 if (currentComboSelection.equals(convertionChoice[3])) {          
                        value1.setText("Kilograms");
                        value2.setText("Grams");
                        indicator = "KG/G";}

           }
       } );

     convert.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed (ActionEvent e) {
             if("K/M".equals(indicator)) {
                   String km1 = one.getText();
                   String m1 = two.getText();
                        if(!km1.isEmpty()) {
                            double km2 = Double.parseDouble(km1);
                            result = km2 * 1000;
                            String result2 = Double.toString(result);
                            two.setText(result2);}
                        else {
                            double m2 = Double.parseDouble(m1);
                            result = m2 / 1000;
                            String result2 = Double.toString(result);
                            one.setText(result2); } }

            if("C/F".equals(indicator)) {
                 String cel = one.getText();
                 double cel2 = Double.parseDouble(cel);
                 String fah = two.getText();
                 double fah2 = Double.parseDouble(fah); 
                 if(!cel.isEmpty()) {    
                            result = (fah2 - 32) * 0.56;
                            String result2 = Double.toString(result);
                            two.setText(result2); }
                        else {   
                            result = cel2 * 1.8 + 32;
                            String result2 = Double.toString(result);
                            one.setText(result2); } }

            if("KG/G".equals(indicator)) {
                   String kg1 = one.getText();
                   String g1 = two.getText();
                        if(!kg1.isEmpty()) {
                            double kg2 = Double.parseDouble(kg1);
                            result = kg2 * 1000;
                            String result2 = Double.toString(result);
                            two.setText(result2);}
                        else {
                            double g2 = Double.parseDouble(g1);
                            result = g2 / 1000;
                            String result2 = Double.toString(result);
                            one.setText(result2); } }
         }
     });

     exit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

     clear.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            one.setText("");
            two.setText("");
        }
    });

    content.add(convertionList);
    content.add(value1);
    content.add(one);
    content.add(blank1);
    content.add(value2);
    content.add(two);
    content.add(convert);
    content.add(clear);
    content.add(exit);

    converter.setSize(700,200);
    converter.setTitle("Converter");
    converter.setVisible(true);

    converter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

谁能告诉我怎么了?因为每当我输入摄氏度值并按下计算按钮时,总会出现该错误。

任何答案、评论或建议将不胜感激:)

4

2 回答 2

0

从您的代码中,我猜您输入的是度数摄氏度,而不是两者。但在这儿

String cel = one.getText();
double cel2 = Double.parseDouble(cel);
String fah = two.getText();
double fah2 = Double.parseDouble(fah); 

您正在解析两者。结果,其中一个是的,无法解析为双精度。您需要先检查哪个不是空的:

if("C/F".equals(indicator)) {
  if(!one.getText().isEmpty()) {   
    String cel = one.getText();
    double cel2 = Double.parseDouble(cel);
    // Processing ...
  } else if(!two.getText().isEmpty()) { 
    String fah = two.getText();
    double fah2 = Double.parseDouble(fah); 
    // Processing ...
  }
}

我没注意,但你已经在用km 和 kg做这件事,这就是为什么它只发生在temperature上。

于 2013-04-11T19:31:50.523 回答
0

我想我已经明白了...

我放置了错误的文本字段,这就是为什么它说它是空的...

所以在此之前,这是我的代码:

String cel = one.getText();
             double cel2 = Double.parseDouble(cel);
             String fah = two.getText();
             double fah2 = Double.parseDouble(fah); 
             if(!cel.isEmpty()) {    
                        result = (fah2 - 32) * 0.56;
                        String result2 = Double.toString(result);
                        two.setText(result2); }
                    else {   
                        result = cel2 * 1.8 + 32;
                        String result2 = Double.toString(result);
                        one.setText(result2); }

如果 cel 不为空,它应该将 cel 的值计算为华氏度,但我将文本字段二的值(即 fah)放在这里,我从未意识到第二个文本框是空的,这就是它有错误的原因一个空字符串..

所以这是我现在的代码:

    String cel = one.getText();
                     String fah = two.getText();
if(!cel.isEmpty()) {
                            double cel2 = Double.parseDouble(cel);
                            result = cel2 * 1.8 + 32;
                            String result2 = Double.toString(result);
                            two.setText(result2);
                        }
                        else {
                            double fah2 = Double.parseDouble(fah);
                            result = (fah2 - 32) * 0.56;
                            String result2 = Double.toString(result);
                            one.setText(result2); }

现在如果 cel 不为空,它将获取 cel 的值并将其转换为华氏温度.. 由于文本框有一个值,它不再显示空字符串错误..

无论如何,感谢大家的帮助...尽管仍然欢迎评论,答案和建议..再次感谢:)

于 2013-04-12T05:34:39.563 回答