2014-01-25 31 views
0

我在做一個計算器,我得到一個NumberFormatExceptionNumberFormatException與計算器

這是我的方法的代碼:

String[] parts = text.getText().split(" + ", 2); 
temporary[0] = Integer.parseInt(parts[0]); 
temporary[1] = Integer.parseInt(parts[0]); 
answer = temporary[0] + temporary[1]; 

這是我的類代碼:

public int answer = 0; 
public int[] temporary = {0, 0}; 

我得到的NFE在這條線:

temporary[0] = Integer.parseInt(parts[0]); 

任何想法爲什麼?

這是我的堆棧跟蹤:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "8 + 9" 
at java.lang.NumberFormatException.forInputString(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at main.Calculator.actionPerformed(Calculator.java:123) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(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) 
+0

getText()返回什麼? –

+2

爲什麼要解析'parts [0]'兩次?你有沒有試過打印它的價值? – Pshemo

+0

當操作數是加法時,爲什麼你會乘以? – Obicere

回答

5

split使用正則表達式,從而split(" + ")將嘗試拆分兩個或更多的連續空間,因爲你的字符串可能沒有這樣的空間也不會被分割,從而parts[0]將持有整個原始字符串。因爲你的代碼將試圖解析類似

Integer.parseInt("123 + 321")` 

會拋出NumberFormatException,因爲它是不正確的整數這種方法可以解析。嘗試在分割中轉義+。您也可以使空間可選。

嘗試用

String[] parts = text.getText().split("\\s*\\+\\s*", 2); 

另外請注意,您試圖解析parts[0]兩次。更改

temporary[1] = Integer.parseInt(parts[0]); 

temporary[1] = Integer.parseInt(parts[1]); 
+0

堆棧跟蹤'NumberFormatException:對於輸入字符串:「8 + 9」'他剛剛發佈確認這是解決方案:) – Navin

2

的第一個參數是split正則表達式。如果你正在尋找一個加號,這個字符串不會完成這項工作,因爲" + "意味着尋找一個或多個空格,然後是另一個空格。也就是說,它會查找2個或更多空格作爲分隔符。所以如果你的輸入字符串是"2 + 2",那麼就沒有2-space序列,所以split將會返回一個包含"2 + 2"的單元素數組作爲字符串。這不是一個數字的正確格式。

要搜索一個空格,然後用加號後面加一個空格:

String[] parts = text.getText().split(" \\+ ", 2); 

然後解決它,所以它當他們問到兩個數相加不乘:) [OK,你這樣做]

注:如果你最終想要讓你的用戶做除了另外的東西,你可能將無法使用split沒有一個相當複雜的正則表達式。這是因爲split不會返回分隔符。如果唯一可能的分隔符是+,那很好,但如果您可以有其他運營商,則需要知道運營商是什麼,因此split將不起作用。您需要使用更一般的正則表達式匹配。看到這個tutorial

0

正如很多人在此指出的那樣,您必須首先檢查您要解析的字符串是否實際包含數字。現在

,如果你的想法是分析評估的表達,這樣的任務的正確路徑是

+0

從堆棧跟蹤中,他試圖分析'8 + 9'​​並在'+'上分割。 – jww

+0

然後他試圖寫出答案,所以他試圖評估表達式 – Leo