【问题标题】:Java String Index Out of Range: -45Java 字符串索引超出范围:-45
【发布时间】:2015-04-01 04:06:10
【问题描述】:

我正在尝试接受来自用户的文件(通过 GUI 中的文件浏览器)并逐行读取文件。基本上我的兴趣是知道在字符位置 60 和 80 之间到底写了什么,因为在此基础上我得出一些结论需要做出一些决定(特别是阅读 rinex.o 文件)。 但我收到错误Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -45 排队:String typeField=line.substring(60,line.length());,无法前进。

提前感谢您的帮助。

  void parseRinexOFile(File inputfile) throws FileNotFoundException, IOException{
       File obsFile= inputfile;
//       if (obsFile.exists()) //Code to test if the file exists.
//               System.out.println("Hello, I exist"+ obsFile.getPath());
       // Create File Stream, Stream Reader and Bufferreader
        streamObs = new FileInputStream(obsFile);
        inStreamObs = new InputStreamReader(streamObs);
        buffStreamObs = new BufferedReader(inStreamObs);
        BufferedReader in = new BufferedReader(new FileReader(obsFile));
        String line="";
        while((line = in.readLine()) != null)
                {
                    String typeField=line.substring(60,line.length());
                    typeField=typeField.trim();
                    if (typeField.equals("RINEX VERSION / TYPE")) {//Check if the observation file identifier is missing else find the version of the file

                    if (!line.substring(20, 21).equals("O")) {

                            // Error if observation file identifier was not found
                            System.err.println("Observation file identifier is missing in file "
                                                            + obsFile.toString() + " header");
                             ver = 0;

                    } else if (line.substring(5, 7).equals("3.")){                                                  
                            ver = 3;                                                        
                    } else if (line.substring(5, 9).equals("2.12")){                                                        
                            ver = 212;                                                      
                    } else {                                                        
                            ver = 2;  
                            System.out.println(" Current version:"+ver);
                    }                                               
                }
                    //System.out.println(line);

                }
                in.close(); 

        } 

【问题讨论】:

    标签: java exception substring


    【解决方案1】:

    发生异常是因为输入行少于 60 个字符。您需要在调用 substring() 之前检查行长。

    【讨论】:

      【解决方案2】:

      您的字符串长度少于 60 个字符,因此您不能将 60 作为第一个参数传递给 String.substring。正确的解决方案取决于您的要求,但一种可能性是line.substring(Math.min(line.length(), 60), line.length())

      【讨论】:

        【解决方案3】:

        您收到此异常是因为您的 line.length() 低于 60,请在执行此操作之前测试字符串的长度:

        String typeField="";
        if(line.length()>60){
          typeField=line.substring(60,line.length());
        }
        

        【讨论】:

          【解决方案4】:

          您超出了String 的限制...

          //this line is throwing the exception...
          String typeField=line.substring(60,line.length());
          

          用这种方式检查字符串是否包含避免异常:

          if (line.contains("RINEX VERSION / TYPE")) {//Check if the observation file identifier is missing else find the version of the file
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-01-15
            • 2014-06-12
            • 2012-06-04
            • 1970-01-01
            • 1970-01-01
            • 2010-10-31
            • 1970-01-01
            相关资源
            最近更新 更多