线程“主”中的异常java.lang.StringIndexOutOfBoundsException:String索引超出范围 [英] Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range

查看:109
本文介绍了线程“主”中的异常java.lang.StringIndexOutOfBoundsException:String索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获得以下代码的 StringIndexOutOfBoundsException 。这是错误

 线程main中的异常java.lang.StringIndexOutOfBoundsException:String索引超出范围:0 
在java.lang.String.charAt(String.java:694)
在Reflector.readConfig(Reflector.java:103)
在Reflector.run(Reflector.java:48)
在Reflector.main(Reflector.java:419)

这里是代码

$ b $


BufferedReader input = null;
String name = null;
String value = null;
String inputLine = null;
dest = new Hashtable();

//打开并读取配置文件
try {
input = new BufferedReader(new FileReader(reflector.conf));
inputLine = input.readLine();
} catch(IOException e){
System.err.println(读取reflector.conf中的错误);
return(-1);
}
//循环直到整个配置文件被读取
while(inputLine!= null){
//跳过注释:
if(inputLine.charAt )$ {
//提取一个名称/值对,并根据名称分支
//
StringTokenizer tokenizer =
new StringTokenizer(inputLine, =);
name = tokenizer.nextToken();
value = tokenizer.nextToken();

if(name == null){
System.out.println(no name);
继续;
} else if(name.equals(MODE)){
if(setMode(value)!= 0){
System.err.println(Error setting mode to+ value) ;
return(-1);
}

}
} else {
System.err.println(跳过无效的配置文件值:
+名称);
}
}
//在配置文件中读取下一行
try {
inputLine = input.readLine();
} catch(IOException e){
System.err.println(读取reflector.conf中的错误);
return(-1);
}
}

//关闭配置文件
try {
input.close();
} catch(IOException e){
System.err.println(Error closing reflector.conf。);
return(-1);
}

//验证配置文件的组合内容
//确定
如果(!isConfigValid()){
系统。 err.println(配置文件不完整);
return(-1);
}
return(0);
}


解决方案

你有空线在配置文件中,因此检查 if(inputLine.charAt(0)!='#')抛出异常。请记住 readLine()不会读取行尾符。



要解决问题,请添加显式检查跳过空行。最简单的修复是做一些如下操作:

  if(!inputLine.isEmpty()&& inputLine.charAt(0) !='#'){


I am getting StringIndexOutOfBoundsException for the following code. Here is the error

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:694)
at Reflector.readConfig(Reflector.java:103)
at Reflector.run(Reflector.java:48)
at Reflector.main(Reflector.java:419)  

Here is code

public int readConfig() {
    // validate the contents of the config file
    BufferedReader input=null;
    String name=null; 
    String value=null; 
    String inputLine=null;
    dest=new Hashtable();

    // open and read the config file
    try {
      input = new BufferedReader(new FileReader("reflector.conf"));
      inputLine=input.readLine();
    } catch (IOException e) {
      System.err.println("Error reading reflector.conf.");
      return(-1);
    }
    // loop until entire config file is read
    while (inputLine != null) {
      // skip comments:
      if (inputLine.charAt(0) != '#') {
        // extract a name/value pair, and branch
        // based on the name:
        StringTokenizer tokenizer = 
                            new StringTokenizer(inputLine,"="); 
        name = tokenizer.nextToken(); 
        value = tokenizer.nextToken(); 

        if (name == null) {
          System.out.println("no name"); 
          continue;
        } else if (name.equals(MODE)) {
          if (setMode(value) != 0) {
            System.err.println("Error setting mode to " + value);
            return(-1);
          } 

          }
        } else {
          System.err.println("Skipping invalid config file value: " 
                             + name);
        }
      }
      // read next line in the config file
      try {
        inputLine=input.readLine();
      } catch (IOException e) {
        System.err.println("Error reading reflector.conf.");
        return(-1);
      }
    }

    // close the config file
    try {
      input.close();
    } catch (IOException e) {
      System.err.println("Error closing reflector.conf.");
      return(-1);
    }

    // validate that the combined contents of the config file
    // make sense
    if (! isConfigValid()) {
      System.err.println("Configuration file is not complete.");
      return(-1);
    }
    return(0);
}

解决方案

You have an empty line somewhere in the config file and thus the check if(inputLine.charAt(0) != '#') throws the exception. Keep in mind readLine() does not read the end of line character.

To fix the problem add explicit check to skip empty lines. Easiest fix is to do something like:

if (!inputLine.isEmpty() && inputLine.charAt(0) != '#') {

这篇关于线程“主”中的异常java.lang.StringIndexOutOfBoundsException:String索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆