2016-10-04 55 views
0

我正在從一個jtextarea的輸入,需要能夠從輸入中讀取多行並輸出計算。我可以閱讀第一行,但之後沒有(崩潰)。這裏是我的代碼,現在Java - 在jtextarea中讀取多行;轉換爲字符數組

 String exp = inputTextArea.getText(); 
     String[] lines = exp.split("\\n"); 
     for (int i = 0; i < lines.length; i++) 
     { 
     InterpObj.p = exp.toCharArray(); 
     InterpObj.counter = 0; 

     double nr = InterpObj.E(); 
     String str=Double.toString(nr); 

     outputTextArea.append(" " +str); 
     } 

以下是錯誤消息

{Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For  input string: "6 12" 
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) 
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) 
at java.lang.Double.parseDouble(Double.java:538) 
at Interpreter.A(Interpreter.java:43) 
at Interpreter.E(Interpreter.java:24) 
at Interp_1a$ButtonListener.actionPerformed(Interp_1a.java:91) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 
at java.awt.Component.processMouseEvent(Component.java:6525) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) 
at java.awt.Component.processEvent(Component.java:6290) 
at java.awt.Container.processEvent(Container.java:2234) 
at java.awt.Component.dispatchEventImpl(Component.java:4881) 
at java.awt.Container.dispatchEventImpl(Container.java:2292) 
at java.awt.Component.dispatchEvent(Component.java:4703) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) 
at java.awt.Container.dispatchEventImpl(Container.java:2278) 
at java.awt.Window.dispatchEventImpl(Window.java:2750) 
at java.awt.Component.dispatchEvent(Component.java:4703) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) 
at java.awt.EventQueue.access$500(EventQueue.java:97) 
at java.awt.EventQueue$3.run(EventQueue.java:709) 
at java.awt.EventQueue$3.run(EventQueue.java:703) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) 
at java.awt.EventQueue$4.run(EventQueue.java:731) 
at java.awt.EventQueue$4.run(EventQueue.java:729) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) 
+0

在你的解釋器類的某個時刻,你試圖把''6 12「'變成一個'double'。它不這樣工作。要麼按空格拆分字符串,要麼在每行上只輸入一個數字。作爲一個附註,我想知道是否InterpObj.p = exp.toCharArray();'實際上應該是'InterpObj.p = lines [i] .toCharArray();' – jonhopkins

+0

第二部分確實是愚蠢的錯誤。謝謝!! @jonhopkins –

回答

0

我認爲你應該檢查你的Interpreter.E()方法...

在Interpreter.E(口譯.java:24)

0

需要將InterpObj.p = exp.toCharArray();更改爲InterpObj.lines[i] = exp.toCharArray();感謝@jonhopkins

相關問題