Java 数组越界异常
我的计算机中有一个文本文件,我正在从java程序中读取它,我想建立一些标准。这是我的记事本文件:Java 数组越界异常,java,string,split,Java,String,Split,我的计算机中有一个文本文件,我正在从java程序中读取它,我想建立一些标准。这是我的记事本文件: #Students #studentId studentkey yearLevel studentName token 358314 432731243 12 Adrian Afg56 358297 432730131 12 Armstrong YU
#Students
#studentId studentkey yearLevel studentName token
358314 432731243 12 Adrian Afg56
358297 432730131 12 Armstrong YUY89
358341 432737489 12 Atkins JK671
#Teachers
#teacherId teacherkey yearLevel teacherName token
358314 432731243 12 Adrian N7ACD
358297 432730131 12 Armstrong EY2C
358341 432737489 12 Atkins F4NGH
当我用下面的代码从记事本上读到这个时,我得到了一个数组超出绑定的异常。调试时,我得到strLine.length()的“ï»#Students”值。
有人能帮忙解决这个问题吗
private static Integer STUDENT_ID_COLUMN = 0;
private static Integer STUDENT_KEY_COLUMN = 1;
private static Integer YEAR_LEVEL_COLUMN = 2;
private static Integer STUDENT_NAME_COLUMN = 3;
private static Integer TOKEN_COLUMN = 4;
public static void main(String[] args) {
ArrayList<String> studentTokens = new ArrayList<String>();
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
strLine = strLine.trim();
if ((strLine.length()!=0) && (strLine.charAt(0)!='#')) {
String[] students = strLine.split("\\s+");
studentTokens.add(students[TOKEN_COLUMN]);
}
}
for (String s : studentTokens) {
System.out.println(s);
}
// Close the input stream
in.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
private静态整数STUDENT\u ID\u COLUMN=0;
私有静态整数STUDENT\u KEY\u COLUMN=1;
私有静态整年\级别\列=2;
私有静态整数STUDENT\u NAME\u COLUMN=3;
私有静态整数令牌_列=4;
公共静态void main(字符串[]args){
ArrayList studentTokens=新的ArrayList();
试一试{
//打开第一个文件
//命令行参数
FileInputStream fstream=新的FileInputStream(“test.txt”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(fstream));
弦斯特林;
//逐行读取文件
而((strLine=br.readLine())!=null){
strLine=strLine.trim();
if((strLine.length()!=0)和&(strLine.charAt(0)!='#')){
字符串[]students=strLine.split(\\s+);
添加(学生[TOKEN_列]);
}
}
for(字符串s:studentTokens){
系统输出打印项次;
}
//关闭输入流
in.close();
}catch(异常e){//catch异常(如果有)
System.err.println(“错误:+e.getMessage());
}
}
考虑到字符集,可能文件被认为是Unicode格式的,但您要求的是ASCII?您可以在此处更改:
BufferedReader br = new BufferedReader(new InputStreamReader(in, charakterset));
这可能会有所帮助:考虑到字符集,可能文件被认为是Unicode格式的,但您要求的是ASCII?您可以在此处更改:
BufferedReader br = new BufferedReader(new InputStreamReader(in, charakterset));
这可能会有所帮助:您似乎面临一些编码问题。以相同的格式保存和读取文件。最好使用UTF-8。使用构造函数
newfileinputstream(,“UTF8”)
进行读取。您似乎面临一些编码问题。以相同的格式保存和读取文件。最好使用UTF-8。使用构造函数
newfileinputstream(,“UTF8”)
进行读取。文件的编码可能与您在中读取的内容不同 找出文件的编码,或者将其转换为
UTF8
,然后在代码中按如下所示的编码将其读入
您还应该更改strLine.charAt(0)!='#'代码>到!strLine.contains(“#”)
,除非保证它是第一个字符并且可能出现在其他字段中
此外,最好调用捕获的任何异常printStackTrace()
public static void main(String[] args) {
ArrayList<String> studentTokens = new ArrayList<String>();
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(new File("C:\\Fieldglass\\workspace-Tools\\Tools\\src\\tools\\sanket.txt"));
// ------ See below, added in encoding, you can change this as needed if not using utf8
BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF8"));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
strLine = strLine.trim();
if ((strLine.length()!=0) && (!strLine.contains("#"))) {
String[] students = strLine.split("\\s+");
studentTokens.add(students[TOKEN_COLUMN]);
}
}
for (String s : studentTokens) {
System.out.println(s);
}
// Close the input stream
fstream.close();
br.close(); // dont forget to close your buffered reader also
} catch (Exception e) {// Catch exception if any
e.printStackTrace();
System.err.println("Error: " + e.getMessage());
}
}
publicstaticvoidmain(字符串[]args){
ArrayList studentTokens=新的ArrayList();
试一试{
//打开第一个文件
//命令行参数
FileInputStream fstream=新的FileInputStream(新文件(“C:\\Fieldglass\\workspace Tools\\Tools\\src\\Tools\\sanket.txt”);
//------请参见下面的编码中添加的内容,如果不使用utf8,您可以根据需要进行更改
BufferedReader br=新的BufferedReader(新的InputStreamReader(fstream,“UTF8”));
弦斯特林;
//逐行读取文件
而((strLine=br.readLine())!=null){
strLine=strLine.trim();
如果((strLine.length()!=0)和(!strLine.contains(“#”){
字符串[]students=strLine.split(\\s+);
添加(学生[TOKEN_列]);
}
}
for(字符串s:studentTokens){
系统输出打印项次;
}
//关闭输入流
fstream.close();
br.close();//不要忘记也关闭缓冲读取器
}catch(异常e){//catch异常(如果有)
e、 printStackTrace();
System.err.println(“错误:+e.getMessage());
}
}
您可以在此处查找(从1.5开始)您的文件编码可能与您在as中读取的不同 找出文件的编码,或者将其转换为
UTF8
,然后在代码中按如下所示的编码将其读入
您还应该更改strLine.charAt(0)!='#'代码>到!strLine.contains(“#”)
,除非保证它是第一个字符并且可能出现在其他字段中
此外,最好调用捕获的任何异常printStackTrace()
public static void main(String[] args) {
ArrayList<String> studentTokens = new ArrayList<String>();
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(new File("C:\\Fieldglass\\workspace-Tools\\Tools\\src\\tools\\sanket.txt"));
// ------ See below, added in encoding, you can change this as needed if not using utf8
BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF8"));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
strLine = strLine.trim();
if ((strLine.length()!=0) && (!strLine.contains("#"))) {
String[] students = strLine.split("\\s+");
studentTokens.add(students[TOKEN_COLUMN]);
}
}
for (String s : studentTokens) {
System.out.println(s);
}
// Close the input stream
fstream.close();
br.close(); // dont forget to close your buffered reader also
} catch (Exception e) {// Catch exception if any
e.printStackTrace();
System.err.println("Error: " + e.getMessage());
}
}
publicstaticvoidmain(字符串[]args){
ArrayList studentTokens=新的ArrayList();
试一试{
//打开第一个文件
//命令行参数
FileInputStream fstream=新的FileInputStream(新文件(“C:\\Fieldglass\\workspace Tools\\Tools\\src\\Tools\\sanket.txt”);
//------请参见下面的编码中添加的内容,如果不使用utf8,您可以根据需要进行更改
BufferedReader br=新的BufferedReader(新的InputStreamReader(fstream,“UTF8”));
弦斯特林;
//逐行读取文件
而((strLine=br.readLine())!=null){
strLine=strLine.trim();
如果((strLine.length()!=0)和(!strLine.contains(“#”){
字符串[]students=strLine.split(\\s+);
添加(学生[TOKEN_列]);
}
}
for(字符串s:studentTokens){
系统输出打印项次;
}
//关闭输入流
fstream.close();
br.close();//不要忘记也关闭缓冲读取器
}catch(异常e){//catch异常(如果有)
e、 printStackTrace();
System.err.println(“错误:+e.getMessage());
}
}
您可以在此处查找(从1.5开始)您提供的信息不准确 当我用下面的代码从记事本上读到这个时,我得到了一个数组超出绑定的异常 我能