从文本字段使用 Double.parseDouble 时出错



我一直在尝试从文本字段中获取输入并将其放入双精度变量中。 但是当我尝试使用Double.parseDouble(textTF.getText())时出现错误时,我使用扫描仪进行了测试,它工作正常

private class NumRTFHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String romanN = "";
        String dec = numDTF.getText();
        Double decimal = Double.parseDouble(dec);
    }
}

我猜可能是动作侦听器,但我不确定如何处理它。 Double.parseDouble()既不适用于textField,也不适用于字符串。

请帮忙。

这些是错误。

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at RomanNumeralConvertert$NumRTFHandler.actionPerformed(RomanNumeralConvertert.java:40)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我在顶部声明了数字DTF

JTextField numDTF;

它从用户那里获得输入。

编辑:

这是整个代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RomanNumeralConvertert extends JFrame {
  private static final int HEIGHT = 60;  
  private static final int WIDTH = 600;
  private JLabel numRL, numDL;  
  private JTextField numRTF, numDTF;
  private NumRTFHandler nrh;
  private NumDTFHandler ndh;
  public RomanNumeralConvertert() {
    setTitle("Roman Numeral to Decimal Converter");                  
    numRL = new JLabel("Decimal Value: ", SwingConstants.RIGHT);
    numDL = new JLabel("Roman Numeral: ", SwingConstants.RIGHT);
    numRTF = new JTextField(10);
    numDTF = new JTextField(10);
    nrh = new NumRTFHandler();
    ndh = new NumDTFHandler();
    numRTF.addActionListener(nrh);
    numDTF.addActionListener(ndh);
    Container pane = getContentPane();            
    pane.setLayout(new GridLayout(1, 4));
    pane.add(numDL); pane.add(numDTF);
    pane.add(numRL); pane.add(numRTF);
    setSize(WIDTH, HEIGHT);                     
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
  private class NumRTFHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String romanN = "";
      String dec = numDTF.getText();
      Double decimal = Double.parseDouble(dec);
      //converting to Roman Numerals 
      while(decimal >= 1000){
        romanN += "M";
        decimal -= 1000.0;  
      }
      while(decimal >= 900){
        romanN += "CM";
        decimal -= 900;  
      }
      while(decimal >= 500){
        romanN += "D";
        decimal -= 500;  
      }
      while(decimal >= 400){
        romanN += "CD";
        decimal -= 400;  
      }
      while(decimal >= 100){
        romanN += "C";
        decimal -= 100;  
      }
      while(decimal >= 90){
        romanN += "XC";
        decimal -= 90;  
      }
      while(decimal >= 50){
        romanN += "L";
        decimal -= 50;  
      }
      while(decimal >= 40){
        romanN += "XL";
        decimal -= 40;  
      }
      while(decimal >= 10){
        romanN += "X";
        decimal -= 10;  
      }
      while(decimal >= 9){
        romanN += "IX";
        decimal -= 9;  
      }
      while(decimal >= 5){
        romanN += "V";
        decimal -= 5;  
      }
      while(decimal >= 4){
        romanN += "IV";
        decimal -= 4;  
      }
      while(decimal >= 1){
        romanN += "I";
        decimal -= 1;  
      }
      numRTF.setText(romanN);
    }
  }
  private class NumDTFHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String romanN = numDTF.getText();
      double decimal = 0;
      romanN.toUpperCase();
      int i=0;
      while(i <= romanN.length()){
        char char1 = romanN.charAt(i);
        i++;
        char char2 ='i';
        if(i < romanN.length()){
          char2 = romanN.charAt(i+1);
          i++;
        }
        if( char1 == ('M')){
          decimal +=1000;
        }
        if( char1 == ('D')){
          decimal +=500;
        }
        if( char1 ==('C')){
          if( char2 == ('M')){
            decimal +=900;
          }
          if( char1 == ('D')){
            decimal +=400;
          }
        }else{
          decimal += 100;
        }
        if( char1 == ('L')){
          decimal +=50;
        }
        if( char1 == ('X')){
          if( char2 == ('C')){
            decimal +=90;
          }
          if( char1 == ('L')){
            decimal +=40;
          }
        }else{
          decimal += 10;
        }
        if( char1 == ('V')){
          decimal +=5;
        }
        if( char1 == ('1')){
          if( char2 == ('X')){
            decimal +=9;
          }
          if( char1 == ('V')){
            decimal +=4;
          }
       }else{
          decimal += 1;
       }
      }
      numDTF.setText("" + decimal);
    }
  }
  public static void main(String[] args) {
    RomanNumeralConvertert gui = new RomanNumeralConvertert();
  }
  // End of the Code
}

我想不通,我检查了逻辑,没有问题。

编辑:

我已经解决了导致空字符串的问题。 有一些文本字段名称切换在一起,只需要更改名称,它现在可以工作了,这里的逻辑几乎没有问题,但也解决了。

您收到empty string错误,从您发布的代码中,我猜调用 getText() 方法无法返回任何内容,因为numDTF没有值。

最新更新