I'm very new to Java and I'm basically teaching myself. I'm writing a program where I need to plot some values. I have a while loop and I need to print x and y values to a text file that I can later graph using gnuplot. If someone can help me make two colums of x and y values to a text file I would really appreciate it!

By columns I assume you mean two fields of data separated by some spaces on the same line. The line ending with a newline character.
When you build the record insert the number of spaces between the two fields and add a newline char at the end:
String rec = dataForCol1 + " " + dataForCul2 + "\n";
If you want the second column to be line up vertically, then you need to compute the number of spaces to insert based on the length of the data in the first column.

...or based on the second column if you want the columns to be right justified.

What sort of input does gnuplot want? Fixed-width, delimited?

I do. I'm not sure what the right question to ask is, but when I tried writing

String rec = x + " " + y + "\n";
it didn't give me an output. I wrote that line in the middle of my while loop.

I also tried doing

System.out.printf("%1$10d%2$11.2f%3$10d%4$11.2f\n", x, y); earlier, and it gave me my x and y values alternating one after another in one list, not in two columns. I'm not sure what goes in the parenthesis before x, y for the printf command. Perhaps that might be my mistake?

Setting a String variable to a value does NOT create any output.
printf() will display the output on the console.

Your printf() args look too complicated for what you need.

System.out.printf("%f    %f\n", x, y);

Edited 6 Years Ago by NormR1: n/a

Whoops! Sorry I didn't notice the rest of your message. The code works perfectly! Thank you so much. what does the %f and %f/n mean?

%f is formatter for float data type.
In Printf() function, you have to specify the formatter for data type - like %d for int, %s for String, %f for float etc. And /n implies new line. After printing each line, it will print a new line i.e. next output will be printed in a new line.

Whoops! Sorry I didn't notice the rest of your message. The code works perfectly! Thank you so much. what does the %f and %f/n mean?

Mark the thread as solved, if your purpose solved.

%f is formatter for float data type.
In Printf() function, you have to specify the formatter for data type - like %d for int, %s for String, %f for float etc. And /n implies new line. After printing each line, it will print a new line i.e. next output will be printed in a new line.

This article has been dead for over six months. Start a new discussion instead.