2015-05-17 23 views
0

我想計算對數值,但空文本字段發生錯誤。如何處理空文本字段的異常?

//Libraries 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.JOptionPane; 
import java.awt.Font; //For Font And Size 
import java.awt.Color; //For Color Change 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class ChildClass extends JFrame { 

    private JTextField textfield1; 
    private JTextField textfield3; 
    private JTextField textfield4; 
    private JButton button1; 

    public ChildClass() { 
     super("Logrithm"); 
     getContentPane().setBackground(new Color(153, 204, 153)); 

     getContentPane().setLayout(null); 

     textfield1 = new JTextField(); 
     textfield1.setBounds(62, 77, 62, 26); 
     getContentPane().add(textfield1); 

     textfield3 = new JTextField(); 
     textfield3.setBounds(127, 224, 153, 26); 
     getContentPane().add(textfield3); 

     textfield4 = new JTextField(); 
     textfield4.setBounds(175, 124, 62, 26); 
     getContentPane().add(textfield4); 

     button1 = new JButton(); 
     button1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       double number1,baseE,result; 
       String text1 = textfield1.getText(); 
       String text3 = textfield4.getText(); 

       if (text1.isEmpty() && text3.isEmpty()) { 
        JOptionPane.showMessageDialog(null, "Enter Values In TextField", "Invalid TextFields", JOptionPane.ERROR_MESSAGE); 
       } 

       if (text3.isEmpty()) { 
        number1 = Double.parseDouble(text1); 
        result = Math.log(number1); 
        textfield3.setText("" + result); 
       } 
       else if (!text3.isEmpty()) { 
        number1 = Double.parseDouble(text1); 
        baseE = Double.parseDouble(text3); 
        result = Math.log(number1)/Math.log(baseE); 
        textfield3.setText("" + result); 
       } 
      } 
     }); 
     button1.setFont(new Font("Bullet", Font.PLAIN, 16)); 
     button1.setText("Click It"); 
     button1.setBounds(161, 172, 87, 26); 
     getContentPane().add(button1); 
    } 
} 

這是主要方法:

public class MainMethod { 

    public static void main(String[] args) { 

     ChildClass obj=new ChildClass(); 

     obj.setSize(450, 400); 
     obj.setResizable(false); 

     obj.setDefaultCloseOperation(obj.EXIT_ON_CLOSE); 
     obj.setLocationRelativeTo(null); 
     obj.setVisible(true); 
    } 
} 

問題我面臨的,當我點擊空文本框將JButton。 Popup MessageDialog出現後,我點擊確定,然後:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String 

如何處理此錯誤?

+0

將剩餘的會話邏輯包含在else if(text1.isEmpty()&& text3.isEmpty()){「 – MadProgrammer

+0

使用JSpinner或JFormattedTextField,它們能夠執行自己的驗證 – MadProgrammer

回答

2

問題我點擊帶有空文本框的jbutton時遇到了問題。彈出MessageDialog進來後,我在線程單擊確定,然後異常「AWT-EventQueue的 - 0」 java.lang.NumberFormatException:空字符串如何如果JTextArea中值爲空(或無效處理此錯誤

- 您可能希望包括有效數字的支票,或使用JFormattedTextField

  1. 向用戶發出警告
  2. 不要試圖處理值(從方法返回,或在剩餘的代碼else子句)

例如:

//note the 'or' rather than 'and' below, presuming both must be valid 
if(text1.isEmpty() || text3.isEmpty()){ 
    JOptionPane.showMessageDialog(null, "Enter Values In TextField", "Invalid TextFields", JOptionPane.ERROR_MESSAGE); 
    return;//return from the method to allow the user to edit the JTextField 
} 
+0

我知道了!感謝隊友的幫助 –

0

java.lang.NumberFormatException拋出當你試圖解析字符串到整數/雙/龍在字符串不以你正在分析的數字格式相匹配。例如, Double.parseDouble("abc");

這裏有三個地方發生。

number1=Double.parseDouble(text1);

number1=Double.parseDouble(text1);

baseE=Double.parseDouble(text3);

如果用戶添加一個字符串不匹配的雙格式會發生這種情況。

+0

我抓住這個東西在我心中感謝幫助! –