1
        while(kb.hasNext())
        {
           array[i] = kb.nextInt();

           System.out.print(array[i] + " ");
        }

I'm reading a text file containing

1 2 3

2 1 3

3 1 2

1 2 3

2 1 3

3 1 2

I'm trying to print it out the same way it is formatted in the text file but I can only print it out like this.

1 2 3 2 1 3 3 1 2 1 2 3 2 1 3 3 1 2

First time asking a question on here so I apologize if it's not clear or poorly worded.

D. Moua
  • 23
  • 6

3 Answers3

5

How about

int count = 0;
while(kb.hasNext()) {
    int i = kb.nextInt();
    if (count++ % 3 == 0) 
        System.out.println(i + " ");
    else 
        System.out.print(i + " ");
}

or if lines were difference length then

while (kb.hasNext ()) {

    String in = kb.nextLine ();

    // either 
    // just print it
    System.out.println(in);

    // or split it and iterate
    String arr[] = in.split (" ");
    for (String i : arr) {
       System.out.print(i + " ");
    } 
    System.out.println(" ");
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

Use System.out.println which will print new line at the end but System.out.print just print the line.

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
0

I figured out what I was doing wrong. But this is what I ended up doing.

        while (kb.hasNextInt()) 
        {          
           String in = kb.nextLine ();
           System.out.println(in + " ");
        }
        System.out.println(" ");
D. Moua
  • 23
  • 6