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!

Recommended Answers

All 12 Replies

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?

Do you have any questions on how to create the file you need?

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

How can I create an output?

What is an output?

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?

Read the API doc for the Formatter class. It is explained there.

%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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.