-1

I need to read input test cases from console into a 2D array and a 1D array but unable to find the error in my code

    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();
        }

    }


}

Also the test case input is as follows

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 

I am able to read the first test case but while reading the 2nd test case i am getting null error.

1 Answers1

1

On your second pass:

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

You create a second BufferedReader from System.in.

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

It looks like you're expecting this to re-read the input. But all the input has already been read.

You might be able to reset this small input stream with a combination of InputStream.readLimit(), InputStream.mark() and InputStream.reset(). Or you could put the data in a file, and create a new FileInputStream for each read.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Suppose if i have a 100 test case which are to be read into same array card[][] and ball[] and for each input test case i need to perform some operation on these arrays then what is the approach?? – Raghavender Mylagary Aug 26 '15 at 18:01
  • One option would be to read the data *once* into `card[]` and `ball[]`, then put the results into parallel arrays. That would eliminate redundant reads and parsing. For this, you might find it helpful to pass references to arrays around as arguments and return values. They don't have to be globals. – Andy Thomas Aug 26 '15 at 18:05
  • HIi i am looking for the code approach for this problem http://www.cs.cmu.edu/~adamchik/15-121/labs/HW-2%20Bingo/lab.html – Raghavender Mylagary Aug 26 '15 at 18:24
  • Okay. This requests that you read from a file, so you can use a new `FileInputStream` for each read. That said, there's no requirement in the instructions to read more than once in the same process. You should use the specified method signatures. They're instance methods, so you can drop `static` off your non-constant declarations except for `main()`, and you'll need to create an object of the class with `new`. Good luck! – Andy Thomas Aug 26 '15 at 18:35