Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException:String index out of range:-1\x05at java.lang.AbstractStringBuilder.substring(Unknown Source)\x05at java.lang.StringBuffer.substring(Unknown Source)\x05at com.briup.ex08.
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/29 01:58:52
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException:String index out of range:-1\x05at java.lang.AbstractStringBuilder.substring(Unknown Source)\x05at java.lang.StringBuffer.substring(Unknown Source)\x05at com.briup.ex08.
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException:String index out of range:-1
\x05at java.lang.AbstractStringBuilder.substring(Unknown Source)
\x05at java.lang.StringBuffer.substring(Unknown Source)
\x05at com.briup.ex08.MyCalculator.actionPerformed(MyCalculator.java:279)
\x05at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
\x05at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
\x05at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
\x05at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
\x05at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
\x05at java.awt.Component.processMouseEvent(Unknown Source)
\x05at javax.swing.JComponent.processMouseEvent(Unknown Source)
\x05at java.awt.Component.processEvent(Unknown Source)
\x05at java.awt.Container.processEvent(Unknown Source)
\x05at java.awt.Component.dispatchEventImpl(Unknown Source)
\x05at java.awt.Container.dispatchEventImpl(Unknown Source)
\x05at java.awt.Component.dispatchEvent(Unknown Source)
\x05at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
\x05at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
\x05at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
\x05at java.awt.Container.dispatchEventImpl(Unknown Source)
\x05at java.awt.Window.dispatchEventImpl(Unknown Source)
\x05at java.awt.Component.dispatchEvent(Unknown Source)
\x05at java.awt.EventQueue.dispatchEvent(Unknown Source)
\x05at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
\x05at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
\x05at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
\x05at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
\x05at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
\x05at java.awt.EventDispatchThread.run(Unknown Source)
不知道是什么错误
检查代码的时候提醒我这行有问题:
op2=Double.parseDouble((str.substring(str.indexOf("+"),str.length())));
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException:String index out of range:-1\x05at java.lang.AbstractStringBuilder.substring(Unknown Source)\x05at java.lang.StringBuffer.substring(Unknown Source)\x05at com.briup.ex08.
String index out of range: -1 这是越界了,应是str.indexOf("+")出错.String 的indexOf方法返回查找字符在字符串中的位置,但是没找到的话就会返回-1(详细看JDK API文档).所以当找不到指定字符时救会出错了,字符串下标从0到str.length(),来个-1自然越界(substring()方法).
最好在处理前做个判断
int index = str.indexOf("+");
if(index!=-1){
op2=Double.parseDouble((str.substring(index,str.length())));
}else
{
System.out.println("找不到指定字符!");
}