Java 如何在代码中消除StringIndexOutOfBoundsException?

Java 如何在代码中消除StringIndexOutOfBoundsException?,java,exception,stringindexoutofbounds,Java,Exception,Stringindexoutofbounds,这是我关于stackoverflow的第一个问题。我想测试一下自己,制作一种迷你编程语言。但自从我决定扩展我的代码以来,我就一直得到StringIndexOutofBoundsException。我在stackoverflow和youtube上做了一些研究,似乎我有一个具体的问题。你能检查一下吗? 提前谢谢你。。。 PS:很抱歉混淆了变量名,我已经用我的母语命名了:P 代码: import java.util.Scanner; 导入java.util.Vector; 公共级科曼达{ 私人线头;

这是我关于stackoverflow的第一个问题。我想测试一下自己,制作一种迷你编程语言。但自从我决定扩展我的代码以来,我就一直得到StringIndexOutofBoundsException。我在stackoverflow和youtube上做了一些研究,似乎我有一个具体的问题。你能检查一下吗? 提前谢谢你。。。 PS:很抱歉混淆了变量名,我已经用我的母语命名了:P 代码:

import java.util.Scanner;
导入java.util.Vector;
公共级科曼达{
私人线头;
私有字符串naziv;
私有字符串参数;
公共科曼达(){
超级();
}
公共科曼达(字符串提示,字符串参数){
超级();
this.tip=tip;
this.parametar=parametar;
}
公共科曼达(字符串提示、字符串naziv、字符串参数){
超级();
this.tip=tip;
this.naziv=naziv;
this.parametar=parametar;
}
公共字符串getTip(){
返回端;
}
公共无效设置提示(字符串提示){
this.tip=tip;
}
公共字符串getNaziv(){
返回纳齐夫;
}
公共无效setNaziv(字符串naziv){
this.naziv=naziv;
}
公共字符串getParameter(){
返回参数;
}
公共void setParameter(字符串Parameter){
this.parametar=parametar;
}
}
公共班机{
公共静态无效执行(矢量代码){
对于(int i=0;i
我的控制台输出:

NNS 0.1 (v0.1.0:1, Oct  7 2017, 18:40:49) [MSC v.1900 64 bit] <--STATUS STRING
print: helloworld <---- ME TYPING PRINT: COMMAND
exec <---- THIS IS THE COMMAND THAT TRIGGERS THE EXCEPTION
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String 
index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at Main.main(Main.java:72)

nns0.1(v0.1.0:1192017年10月7日,18:40:49)[MSC v.1900 64位]在
execute
方法中,有如下内部for循环

for (int k = 0; k < code.size(); i++)
for(int k=0;k
这里您正在递增
i
而不是
k
。您正在使用
i
调用
substring
。我认为问题就在这里。因为for循环永远不会结束,最终会有一个
i
值,您可以从您的问题中获得
indexootfound

由于
exec
不包含空格,
line.indexOf(“”)
返回-1,而
line.substring(0,-1)
由于第二个参数为负而失败。您无法访问
exec
的检查,因为它是在抛出异常的检查之后进行的

将要检查的
if(line.equalsIgnoreCase(“exec”)
移动到需要空格的条件之前,例如

if (line.equalsIgnoreCase("exec")) {
  // ...
} else if (line.substring(0, line.indexOf(' ')).equalsIgnoreCase("print: ")) {
  // ...
} else if (line.substring(0, line.indexOf(' ')).equalsIgnoreCase("var")) {
  // ...
}
尽管您可能希望将
line.indexOf(“”)
的值提取到一个变量中,并显式检查该值是否为非负,但是您可以避免在t
exec <---- THIS IS THE COMMAND THAT TRIGGERS THE EXCEPTION
if (line.substring(0, line.indexOf(' ')).equalsIgnoreCase("print: ")) {
  // ...
} else if (line.substring(0, line.indexOf(' ')).equalsIgnoreCase("var")) {
  // ...
} else if (line.equalsIgnoreCase("exec")) {
  // ...
}
if (line.equalsIgnoreCase("exec")) {
  // ...
} else if (line.substring(0, line.indexOf(' ')).equalsIgnoreCase("print: ")) {
  // ...
} else if (line.substring(0, line.indexOf(' ')).equalsIgnoreCase("var")) {
  // ...
}
if (line.equalsIgnoreCase("exec")) {
  // ...
} else {
  int spacePos = line.indexOf(' ');
  if (spacePos >= 0) {
    if (line.substring(0, spacePos).equalsIgnoreCase("print: ")) {
      // ...
    } else if (line.substring(0, spacePos).equalsIgnoreCase("var")) {
      // ...
    }
  }
}
static boolean startsWithIgnoreCase(String line, String cmd) {
  return line.regionMatches(
      true         /* ignore case */,
      0            /* from start of line */,
      cmd          /* the thing you're searching for */,
      0            /* from start of cmd */,
      cmd.length() /* all of cmd */);
}
// Extra space appended because you were searching for the space after the command.
if (startsWithIgnoreCase(line, "print: " + " ")) {
  // ...
} else if (startsWithIgnoreCase(line, "var" + " ")) {
  // ...
} else if (line.equalsIgnoreCase("exec")) {
  // ...
}