-1

入力テスト ケースをコンソールから 2D 配列と 1D 配列に読み込む必要がありますが、コードでエラーを見つけることができません

    import java.util.*;
    import java.lang.*;
    import java.io.*;


    class Ideone
    {
    public static final int SIZE = 5;
    public static int card[][];
    public static int ball[];

    public static void read()throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int count=0;
        for(int row = 0; row < SIZE && count < 25; row++){

            String line= br.readLine();
            System.out.println("line"+line);
            //if((line= br.readLine()) != null) {
            int column = 0;
            StringTokenizer st = new StringTokenizer(line);
            while(st.hasMoreTokens()){
                card[row][column] = Integer.parseInt(st.nextToken());
                column++;
                count++;
            }
            column = 0;
        //  }
        }




        int i = 0, b=0;
        String line ;
        while((line =br.readLine() )!= null && i < 75) {

            StringTokenizer st1 = new StringTokenizer(line);

            while(st1.hasMoreTokens()){

                ball[i++]= Integer.parseInt(st1.nextToken());
            }

        }

    }

    public static void printing() {
        System.out.println("Card values\n");
        for(int i=0;i<SIZE;i++) {
            for(int j=0;j<SIZE;j++){
                System.out.print(card[i][j]+"\t");
            }
            System.out.println();
        }

        System.out.println("Ball values\n");
        for(int j=0;j<75;j++) {

            System.out.print(ball[j]+"\t");

            //System.out.println();
        } 
    }


    public static void main (String[] args) throws java.lang.Exception
    {
        card = new int[SIZE][SIZE];
        ball = new int[75];
        for(int t=0 ; t<2 ; t++) {
            read();
            printing();
        }

    }


}

また、テストケースの入力は次のとおりです

10 17 44 59 62 
2 28 31 58 68 
5 16 37 53 71 
6 26 35 51 66 
9 21 34 60 70 
45 37 19 47 16 39 66 14 28 15 
72 17 62 12 55 11 73 75 9 18 
56 4 29 32 61 63 51 38 33 2 
8 36 6 24 23 22 21 5 60 35 
41 74 34 7 67 25 50 10 43 53 
3 46 68 40 48 69 54 30 20 70 
31 59 57 49 1 42 58 27 52 13 
64 44 71 26 65
1 7 4 9 2 
2 28 31 58 68 
5 16 37 53 71 
6 26 35 51 66 
9 21 34 60 70 
45 37 19 47 16 39 66 14 28 15 
72 17 62 12 55 11 73 75 9 18 
56 4 29 32 61 63 51 38 33 2 
8 36 6 24 23 22 21 5 60 35 
41 74 34 7 67 25 50 10 43 53 
3 46 68 40 48 69 54 30 20 70 
31 59 57 49 1 42 58 27 52 13 
64 44 71 26 65 

最初のテスト ケースを読み取ることができますが、2 番目のテスト ケースを読み取っているときに null エラーが発生します。

4

1 に答える 1

1

2 回目のパスで:

for(int t=0 ; t<2 ; t++) {
    read();

System.in から 2 つ目の BufferedReader を作成します。

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

これが入力を再読み込みすることを期待しているようです。しかし、すべての入力はすでに読み取られています。

InputStream.readLimit()と を組み合わせて、この小さな入力ストリームをリセットできる場合がInputStream.mark()ありInputStream.reset()ます。FileInputStreamまたは、データをファイルに入れ、読み取りごとに新しいファイルを作成することもできます。

于 2015-08-26T17:47:41.590 に答える