簡體   English   中英

線程“ AWT-EventQueue-0”中的異常java.lang.NumberFormatException:空字符串:Java中的錯誤

[英]Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: empty String: Error in Java

我做了一個簡單的Java轉換器,它將KM轉換為M,將Celsius轉換為華氏度,將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);
}

}

誰能告訴我怎么了? 因為每當我輸入攝氏溫度值並按下計算按鈕時,總是會出現該錯誤。

任何答復,評論或建議將不勝感激:)

從您的代碼中,我想您輸入的是度數攝氏度,而不是兩者都輸入。 但在這兒

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進行了此操作,這就是為什么它僅在溫度下發生的原因。

我想我已經明白了...

我放置了錯誤的文本字段,這就是為什么它說為空的原因...

所以之前,這是我的代碼:

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的值計算為華氏度,但我在此處將文本字段2的值(即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的值並將其轉換為華氏度。.由於文本框中有一個值,因此不再顯示空字符串錯誤。

無論如何,謝謝你們的幫助。盡管如此,仍然歡迎評論,答案和建議..再次感謝:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM