KM を M に、摂氏を華氏に、KG を G に変換する単純な Java コンバーターを作成しました。キロメートル<->メートルとキログラム<->グラムの変換は問題ないようです。しかし、温度の値を入力すると、常に次のエラーが発生します。
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);
}
}
誰が何が悪いのか教えてもらえますか? 摂氏の値を入力して計算ボタンを押すたびに、そのエラーが常に表示されるからです。
回答、コメント、または提案は大歓迎です:)