Question

I need to read data of the format:
2
4
7
6 4
2 5 10
9 8 12 2

the data needs to be from the default input stream, System.in. I'm using an InputStreamReader wrapped in a BufferedReader, so I am able to do line by line reading.

InputStreamReader isr=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(isr);
    String x=in.readLine();
    int numcase=Integer.parseInt(x);
    for(int i=1;i<=numcase;i++){
        System.out.println("");
        x=in.readLine();
        int n=Integer.parseInt(x);
        int[][]t=new int[n+1][n+1];
        for(int j=1;j<=n;j++){
            System.out.println();
            x=in.readLine();
            StringTokenizer st=new StringTokenizer(x);
            for(int k=1;k<=1;k++){
                x=st.nextToken();
                int nume=Integer.parseInt(x);
                t[j][k]=nume;
            }
        }

the problem is that if the blank System.out.println statement isnt there, it refuses to read in a line of input past reading the first two lines. Thing is, where I'm submitting the code monitors the output stream and as such will grade my output as incorrect by outputting the wrong answer.

Was it helpful?

Solution 2

I am not sure what you are expecting but writing to system.out has no effect on what you read from system.in.

That said some pointers about your code:

 String x=in.readLine();
 int numcase=Integer.parseInt(x);
 for(int i=1;i<=numcase;i++){

this loop will be entered twice.

x=in.readLine();
int n=Integer.parseInt(x);
int[][]t=new int[n+1][n+1];

n will be 4 and the array will be of size 5 by 5. Why? I cant understand the reasoning behind this.

 for(int j=1;j<=n;j++){

this loop will be executed 4 times. Reading one line every time.

 StringTokenizer st=new StringTokenizer(x);
        for(int k=1;k<=1;k++){

This loop will always be entered only ONCE even though there may be more than one token.

int nume=Integer.parseInt(x);
            t[j][k]=nume;

since k is always 1, you will only fill in one of the row/column in your 2d array.

Conclusion. print statements are doing no magic.

The code is doing what it is supposed to do.

Think about what you are attempting to do and print out the values of variables to confirm the behaviour as you code.

I am pretty sure that you are attempting to fill the 2d array with numbers and the problem lies with the condition over k but since I do not really know what you are trying to do, I cant be 100% sure.

Edit

command line

OTHER TIPS

  • Okay first of all, never use StringTokenizer, as it is deprecated.
  • Secondly, arrays are zero indexed. So use for (i = 0, i < size; i++) not i=1 and i <= size. I'm going to guess your last programming language was Visual Basic.
    • Your array is too big for the size of your input data. Use new int[n][n] with the above zero indexing.
  • Your two dimensional array is going to go out of scope as soon as you iterate over the for loop again.
  • If you are going to use readLine, you will be a lot better off with String.split(" ")

If you can clarify more about what the input format needs to be and the required output, I can help you to read it better.

Nick Name

Given your data set, here's how you're reading the input...

2line 3
4line 7
7line 12 after this line, you try to tokenize x (which contains only "7"), but then you break
6 4                     out of the k loop to read the rest of your input (in a really weird way)
2 5 10
9 8 12 2

I'm just curious if you are supposed to save the tokens in [row][cols] on that t[][] array!

 1 InputStreamReader isr=new InputStreamReader(System.in);
 2  BufferedReader in=new BufferedReader(isr);
 3  String x=in.readLine();
 4  int numcase=Integer.parseInt(x);
 5  for(int i=1;i<=numcase;i++){
 6      System.out.println("");
 7      x=in.readLine();
 8      int n=Integer.parseInt(x);
 9      int[][]t=new int[n+1][n+1];
10      for(int j=1;j<=n;j++){
11          System.out.println();
12          x=in.readLine();
14          StringTokenizer st=new StringTokenizer(x);
15          for(int k=1;k<=1;k++){
16              x=st.nextToken();
17              int nume=Integer.parseInt(x);
18              t[j][k]=nume;
19          }
20      }
21
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top