gpt4 book ai didi

Java字符串索引超出范围: -45

转载 作者:太空宇宙 更新时间:2023-11-04 14:11:27 24 4
gpt4 key购买 nike

我正在尝试接受来自用户的文件(通过 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();

}

最佳答案

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

关于Java字符串索引超出范围: -45,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28273875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com