2016-12-31 195 views
-1

我一直在尋找其他帖子的答案,他們似乎指向我在做什麼,但我仍然得到NumberFormatException。我試圖從JFrame通過拉jtextfield值並將其轉換爲int來構建一個簡單的計算器,但我得到以下異常。將jtextfield轉換爲int的數字格式例外

package com.calculator.app; 

import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class CalculatorApp extends JFrame { 

    private JPanel contentPane; 
    private JTextField textField = new JTextField(); 
    private JTextField textField_1 = new JTextField(); 
    private JTextField textField_2 = new JTextField(); 
    int num1 = Integer.parseInt(textField.getText()); 
    int num2 = Integer.parseInt(textField_1.getText()); 
    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        CalculatorApp frame = new CalculatorApp(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public CalculatorApp() { 

    // int num2 = Integer.parseInt(value2);  

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     JLabel lblCalculatorApp = new JLabel("Calculator App"); 
     lblCalculatorApp.setBounds(141, 37, 138, 23); 
     contentPane.add(lblCalculatorApp); 

     JButton btnNewButton = new JButton("Add"); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 

      } 
     }); 
     btnNewButton.setBounds(74, 112, 131, 31); 
     contentPane.add(btnNewButton); 

     JLabel lblNewLabel = new JLabel(""); 
     lblNewLabel.setBounds(169, 181, 82, 23); 
     contentPane.add(lblNewLabel); 

     textField = new JTextField(); 
     textField.setBounds(55, 73, 54, 23); 
     contentPane.add(textField); 
     textField.setColumns(10); 

     textField_1 = new JTextField(); 
     textField_1.setColumns(10); 
     textField_1.setBounds(151, 70, 54, 23); 
     contentPane.add(textField_1); 

     textField_2 = new JTextField(); 
     textField_2.setColumns(10); 
     textField_2.setBounds(109, 162, 124, 55); 
     contentPane.add(textField_2); 
    // textField_2.setText(String.valueOf(output)); 
     try{ 

      int output = Integer.parseInt(textField.getText()); 
       } 
       catch(NumberFormatException e){ 
        System.out.println("Error");  
       } 

    } 
} 

異常

java.lang.NumberFormatException: For input string: "" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at com.calculator.ap.CalculatorApp.<init>(CalculatorApp.java:23) 
    at com.calculator.ap.CalculatorApp$1.run(CalculatorApp.java:32) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$500(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$JavaSecurityAccessImpl.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) 
+1

發佈異常詳細信息,但可能this'Integer.parseInt(textField.getText());'有問題,因爲這個'extField。的getText()'是不是給你正確的格式化整數 –

+0

我張貼例外上面 – user6680

+0

不能解析一個空字符串爲一個數字,字符串值必須包含數字只爲解析成功 – Dummy

回答

5

這條線:

int num1 = Integer.parseInt(textField.getText()); 

相當於

int num1 = Integer.parseInt(""); 

,因爲你沒有在該行特定的值初始化textField

private JTextField textField = new JTextField(); 

所以,你正在試圖解析一個空字符串作爲整數,但它拋出NumberFormatException,因爲它不是一個整數。

如果你只是想獲得對這些問題,你可以把3個整數有:

private JTextField textField = new JTextField("1"); 
private JTextField textField_1 = new JTextField("2"); 
private JTextField textField_2 = new JTextField("3"); 

或(更好的),你可以刪除下列行,因爲它似乎你需要在以後對其進行解析(當按鈕被點擊):

int num1 = Integer.parseInt(textField.getText()); 
int num2 = Integer.parseInt(textField_1.getText()); 

上面的解釋可以幫助你避免例外。

但是,如果你要總結這些數字,你應該填寫此方法的主體:

btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      String text1 = textField.getText(); 
      String text2 = textField_1.getText(); 
      textField_2.setText(getIntegerValue(text1) + getIntegerValue(text2) + ""); 
     } 
    }); 

其中getIntegerValue可以是這樣的:

private int getIntegerValue(String s) { 
    if ("".equals(s)) { 
     return 0; 
    } 
    return Integer.parseInt(s); 
} 

或更短:

private int getIntegerValue(String s) { 
    return "".equals(s) ? 0 : Integer.parseInt(s); 
} 

此方法:

  • 返回0如果字符串是空的(沒有值)
  • 如果字符串表示整數
  • 拋出NumberFormatException如果字符串不是整數或空字符串
  • 不需要返回整數抓NumberFormatException,因爲它是一個RuntimeException

當然,你可以,如果你想扔從最後的方法去除if /三元運算符即使字符串爲空也是一個例外。

+0

感謝。這似乎工作。所以沒有任何內置的,我可以不寫我自己喜歡你getIntegerValue沒有得到這個工作使用的方法呢? – user6680

+0

我不知道另一種方法。如果您想停止某些輸入的異常,我認爲您應該在純Java中手動執行此操作(不使用外部庫)。現在,根據您的需要,您可以在之前驗證輸入(最終使用正則表達式)。 –