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
xOF7ڋD#4u JF雋sN9wMJJM5Ut(!?cW =ۡiTesC|s2ҙJb)I)4rzm&2Qt $݂! NiFUI4٤2JL4>byA $b2(B4 |Yl$a0 eFMUK" Od4ML*1NQ$T5ur߂x{;`Hwɦ@L!A'g}wIY#Q1R4r/1 qRCuIC 46l$֋E=DנPe0^}5`@34HC &Y.Nh]S" /.kn=]$R̠~ I!Ӟv(b;aa+e0}J_2iy.@73$KpK5zyGa]VTN|ܗDPjY5?YZ\mj$gUGXlj>yJ8u(hX:~}8.gR4&In_ykg.磹m3(d{/-5/ڏ:cv6j,8>Qxv]Zz#&'?OJK^p^*L"_+^[x(h/t_|^<6EuJRv^Ec+gnO0qkypc|1+{"v_S>$My Ec!=wAwf S5&́;~'

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("找不到指定字符!");
}