0
        int N = scan.nextInt();
        scan.next();
        
        String x = "";
        for(int i = 0; i < N; i++)
        {
            x = scan.nextLine();
            String array[] = x.split(" ");
            System.out.println(array[0] + "\n" + array[1]);
        }

This is the problematic code. I have scan.next() after scanning the integer N since I know that if I don't have it there, it'll read the space next to the integer that I type in. If I type in '6', the program would first register 6 as the value for N but then read 6_ (_ is the cursor) and then throw an error.

This does work but it messes up the splitting of the string 'x'. This is the sample input:

3
0 100
1 124
2 42

and this is the desired output:

0
100
1
124
2
42

Of course, the output gets spit out every time I enter a new line since I have the System.out.println() inside the for loop but anyway, the problem is that the first digit is not printed out. There's just a blank space where the (in this case) '0' should be. If I move the scan.next() inside the for loop (but at the very start of the loop), there's a blank space for every first digit (0, 1, and 2 are replaced by a space). If I don't include the scan.next() at all, I get a java.lang.ArrayIndexOutOfBoundsException error.

How can I solve this? Can't really mess with the input so I have to manipulate the code somehow.

Tom
  • 14,120
  • 16
  • 41
  • 47

0 Answers0