2

Here is my code for reading a txt file, but I can't seem to properly arrange the output of the file content.

This is the file content:

venti lador 13 male taguig

pito dingal 26 male pateros

dony martinez 24 male hagonoy

package testprojects;

import java.io.BufferedReader;
import java.io.FileReader;

public class ReadTextFile {
    public static void main(String args[]) throws Exception {
        BufferedReader bf = new BufferedReader(new FileReader("C://Users/PAO/Desktop/sampletxt.txt"));
        String line;

        while ((line = bf.readLine()) != null) {
            String[] value = line.split(" ");

            for (int x = 0; x < value.length; x++) {
                if (x >= 5) {
                    System.out.println();
                }
                System.out.print(value[x] + " ");
            }
        }
    }
}

Output:

venti lador 13 male taguig pito dingal 26 male pateros dony martinez 24 male hagonoy

Desired output:

venti lador 13 male taguig

pito dingal 26 male pateros

dony martinez 24 male hagonoy

Tom
  • 16,842
  • 17
  • 45
  • 54
  • Please ensure your question is formatted properly in order for us to understand what your current output is and what your desired output is. Line breaks are getting lost in translation and it is almost impossible to follow. – Claudio Apr 21 '15 at 16:14
  • How many elements are in value after you perform the split? Do you want all the elements to be on the same line or broken apart to multiple lines? How many items per line do you want? – Shar1er80 Apr 21 '15 at 16:16
  • Be clearer on what your inputs and what you want your outputs to be. Now that you have "working" code, go into your sampletxt.txt and break apart the lines, in the file, into more lines and will you still get the expected results that you want? It's good that you got your code to work, but it's better to understand (for learning) other scenarios of data that you could potentially have and your code can handle all scenarios. – Shar1er80 Apr 21 '15 at 17:27

2 Answers2

3

Change your condition to

if((x + 1) % 5 == 0 || (x == value.length - 1))

So that it will skip line at each 5 datas or if your reach the last data of your row.

You would have something like this :

System.out.print(value[x] + " " );
if((x + 1) % 5 == 0 || (x == value.length - 1))
    System.out.println();
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
1

You don't need the if statement for a line break, just put your println() after your for loop.

public static void main(String[] args) throws Exception {    
    BufferedReader bf = new BufferedReader(new FileReader("C://Users/PAO/Desktop/sampletxt.txt"));

    String line;

    while((line=bf.readLine())!=null){
        String[] value=line.split(" ");

        for (String value1 : value) {
            System.out.print(value1 + " ");
        }
        System.out.println(""); // Goes to the next line after printing all values
    }
}

If you're wanting a certain number of values per line (5 for example) try the following:

public static void main(String[] args) throws Exception {    
    BufferedReader bf = new BufferedReader(new FileReader("C://Users/PAO/Desktop/sampletxt.txt"));

    String line;

    int count = 0;
    while((line=bf.readLine())!=null){
        String[] value=line.split(" ");

        for (String value1 : value) {
            System.out.print(value1 + " ");
            count++;

            if (count == 5) { // Five values per line before a line break
                System.out.println("");
                count = 0;
            }
        }
    }
}

If you're just printing out what is in the file, I don't see why you have to perform the split. You're splitting on a single space and then you're printing the values back out with a space in between each value. Just print the line that was read from the file.

public static void main(String[] args) throws Exception {    
    BufferedReader bf = new BufferedReader(new FileReader("C://Users/PAO/Desktop/sampletxt.txt"));

    String line;
    while((line=bf.readLine())!=null){
        System.out.println(line);
    }
}
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • It seems that he wants to do that. I mean, to print a line break every x amount of items. – Claudio Apr 21 '15 at 16:14
  • @matinikestugante My edits should give you want you're looking for then. – Shar1er80 Apr 21 '15 at 16:23
  • If you're wanting 5 values per line, that a little difficult to do using your for loop counter. Say the first line your read from your file has 3 values in it. So far you've printed 3 values on one line. Then the next line you read from your file has 5 values, you'll end up writing those 5 values on the same line as the previous 3 values. Is that what you're wanting? If not, you should track the number of values written on a line with a separate counter. – Shar1er80 Apr 21 '15 at 16:28
  • Yea i accept your suggested edit. btw your way of making a code is sick! – matinik estugante Apr 21 '15 at 16:28