java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Exceptioninthread"AWT-EventQueue-0"java.lang.StringIndexOutOfBoundsException:Stringin...
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.AbstractStringBuilder.substring(Unknown Source)
at java.lang.StringBuffer.substring(Unknown Source)
at com.briup.ex08.MyCalculator.actionPerformed(MyCalculator.java:279)
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.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)
不知道是什么错误
检查代码的时候提醒我这行有问题:
op2=Double.parseDouble((str.substring(str.indexOf("+"),str.length()))); 展开
at java.lang.AbstractStringBuilder.substring(Unknown Source)
at java.lang.StringBuffer.substring(Unknown Source)
at com.briup.ex08.MyCalculator.actionPerformed(MyCalculator.java:279)
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.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)
不知道是什么错误
检查代码的时候提醒我这行有问题:
op2=Double.parseDouble((str.substring(str.indexOf("+"),str.length()))); 展开
3个回答
展开全部
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("找不到指定字符!");
}
最好在处理前做个判断
int index = str.indexOf("+");
if(index!=-1){
op2=Double.parseDouble((str.substring(index,str.length())));
}else
{
System.out.println("找不到指定字符!");
}
追问
if(txt1.getText().trim().indexOf("+")!=-1){
op2=Double.parseDouble((str.substring(str.indexOf("+"),str.length())));
String ss = Double.toString(op1+op2);
txt1.setText(ss);
}
这是我的代码,有判断不是-1的情况的呢
追答
如果是这行代码出错的话,也就是那能出越界了,不过看你写的也判断过了,奇怪了。你确定txt1.getText().trim()和str引用一样?要还一样,直接断点跟踪调试,看看出了什么问题。这我也就看不出其他什么了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询