public class dataarrange {
public static void main(String args[]) {
try {
PrintStream myconsole = new PrintStream(new File("D://out.txt"));
for (int i = 0; i < 10; i++) {
double a = Math.sqrt(i);
int b = 10 + 5;
double c = Math.cos(i);
myconsole.print(a);
myconsole.print(b);
myconsole.print(c);
}
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
}
}
In this programming code I generate a text file named out
where I write down the output of dataarrange class.
There is no error in code. As per code we get a,b,c for 10 times. I write down the value in a systematic way in the text file. The text file should look like a matrix which have 10 rows and 3 columns. But when I open the text file out.txt all data are in scatter way. They are written as a line not as a matrix format.
Desired output:
a b c
val1 val2 val3
val4 val5 val6
val7 val8 val9
So on...
But getting output val1 val2 val3 val4 val5 val6
. How could I fix this problem?
You may also use the escape sequences \n \t but the above anwser with formatted string should be preferred
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class DataRange {
public static void main(String args[]) {
try {
PrintStream myconsole = new PrintStream(new File("out.txt"));
for (int i = 0; i < 10; i++) {
double a = Math.sqrt(i);
int b = 10 + 5;
double c = Math.cos(i);
System.out.print("\t" + a);
myconsole.print("\t" + a);
System.out.print("\t" + b);
myconsole.print("\t" + b);
System.out.print("\t" + c);
myconsole.print("\t" + c);
myconsole.print("\n");
System.out.println("\n");
System.out.println("Completed");
}
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
}
}