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