StringTokenizer不使用BufferedReader

时间:2014-02-23 18:28:35

标签: java bufferedreader stringtokenizer

我正在尝试将StringTokenizer与BufferedReader一起使用,但是我收到了一个错误。     它在我使用Scanner时有效,但当我改为BufferedReader时,我无法使其工作。我需要你输入我的代码有什么问题。这就是我的源文件的样子:

Stud Qu1 Qu2 Qu3 Qu4 Qu5 
1234 052 007 100 078 034 
2134 090 036 090 077 030 
3124 100 045 020 090 070 
4532 011 017 081 032 077 
5678 020 012 045 078 034 
6134 034 080 055 078 045 
7874 060 100 056 078 078 
8026 070 010 066 078 056 
9893 034 009 077 078 020 
1947 045 040 088 078 055 
2877 055 050 099 078 080 
3189 022 070 100 078 077 
4602 089 050 091 078 060 
5405 011 011 000 078 010 
6999 000 098 089 078 020 

And this is the error I am getting:
Exception in thread "main" java.lang.NullPointerException
    at lab4.Student.<init>(Student.java:26)
    at StudentReportApp.main(StudentReportApp.java:6)

import lab4.*;

public class StudentReportApp {
    public static void main (String [] args){
        String fileName = "input.txt";
        Student studentData = new Student(fileName);
        //studentData.findReport ();
        //studentData.printReport ();
    }
}

public Student (String FileName){
        File file = new File(FileName);

        try (BufferedReader br = new BufferedReader(new FileReader(file))){
            String line;

            br.readLine(); //skips first line
            int r = 0;
            while ((line = br.readLine())!= null){
                StringTokenizer stToken = new StringTokenizer(line, " ");
                while(stToken.hasMoreTokens()){
                    student[r] = Integer.parseInt(stToken.nextToken());
                    for (int c = 1; c < 6; c++)
                        grade [r][c-1] = Integer.parseInt(stToken.nextToken());      
                    r++;
                }
                arrayStudents++;
            }

            student = new int [arrayStudents];
            grade = new int[arrayStudents][5];

        } catch (FileNotFoundException e) {
            System.out.println("Can't find file " + file.toString());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Unable to read file " + file.toString());
            e.printStackTrace();
        }
    }

3 个答案:

答案 0 :(得分:1)

您需要初始化整数数组,以便可以将值分配给其各个元素

int[] student = new int[100];
int[][] grade = new int[100][100];

(也就是说,数组是固定大小的,考虑使用更灵活的集合,例如ArrayList

答案 1 :(得分:1)

grade [r][c-1] = Integer.parseInt(stToken.nextToken());      

在这段代码中,你试图再次访问nextToken ..这会将tokenizer向前移动一步。

相反,您应首先检查该令牌是否可用,然后访问令牌。

正确的方式:

if(stToken.hasNextToken())
{
 grade[r][c-1] = Integer.parseInt(stToken.nextToken());   
 }

答案 2 :(得分:1)

如你所见

br.readLine(); //skips first line
int r = 0;
while ((line = br.readLine())!= null){
    StringTokenizer stToken = new StringTokenizer(line, " ");
    while(stToken.hasMoreTokens()){
        student[r] = Integer.parseInt(stToken.nextToken());
        for (int c = 1; c < 6; c++)
            grade [r][c-1] = Integer.parseInt(stToken.nextToken());
        r++;
    }
    arrayStudents++;
}

student = new int [arrayStudents];
grade = new int[arrayStudents][5];

您首先将数据放入grandestutent,然后将数据初始化。

student = new int [arrayStudents];
grade = new int[arrayStudents][5];

在任何使用它之前应该去。所以它将是

student = new int [arrayStudents];
grade = new int[arrayStudents][5];

br.readLine(); //skips first line
int r = 0;
while ((line = br.readLine())!= null){
    StringTokenizer stToken = new StringTokenizer(line, " ");
    while(stToken.hasMoreTokens()){
        student[r] = Integer.parseInt(stToken.nextToken());
        for (int c = 1; c < 6; c++)
            grade [r][c-1] = Integer.parseInt(stToken.nextToken());
        r++;
    }
    arrayStudents++;
}

无论如何,你现在不能使用arrayStudents,因为你不知道要添加多少学生..你可以使用ArrayList来更快地避免这个问题。 您也可以使用类来改进代码并使所有内容更有条理。