variable. You can do this, too, with concatenation:
The
+ operator, when used with
Strings, will concatenate two
Strings (or variables, converted to
Strings).
Now, the next step: You want to format the array values into columns. You'll need to make them fit a certain width. If you are using more than just numbers (I'm assuming that
name isn't a number), you can't use a number formatting formatting function (like
nf()). And, without delving too deep into Java, you'll have to write your own.
For example:
- String padString(String value, int spaces) {
- if(value.length() >= spaces) return value; // If there are already enough characters, simply return what was given
- for(int i = value.length(); i < spaces; i ++) //Cycle through all of the missing spaces...
- value += " "; //...and add a space
-
- return value; //Return the formatted String
- }
This function is simple enough. We can use it to format the output so that it lines up.
Note: String.length() is a function of String that returns the length of the String.
First, you'll need to use
print() instead of
println() (so that you can have more than one value in each line). That is simple enough. Then, you'll have to decide how many digits to pad. I'll go with five for the purpose of demonstration (and two more for spacing between columns). For example:
- output.print(padString(name[i], 7)); //Prints the first value (with column spacing)
- output.print(stim[i]); //Prints the second value
When using a
PrintWriter, there's one last thing to remember: you must call
flush() and
close() (on the instance) to finish writing and clean up. For example:
- output.flush();
- output.close();
Now, it's time to go back to your original code.
Given that
ntrials and all of your arrays are valid, we can convert this quite easily:
- void setup() { //Must place the code within a function (setup()) so that we can use our utility function and not get an active / static modes error
- PrintWriter output = createWriter("output.txt"); //Create a PrintWriter to print to "output.txt"
-
- for(i=0;i<ntrials;i++) {
- output.print(padString(name[i], 7)); //Print name[i] to output
- output.print(padString(stim[i], 7)); //Print stim[i] to output
- output.print(padString(resp[i], 7)); //Print resp[i] to output
- output.print(padString(sd[i], 7)); //Print sd[i] to output
- output.print(padString(rho[i], 7)); //Print rho[i] to output
- output.println(con[i]); //Print con[i] to output and create a new line
- }
-
- output.flush();
- output.close();
- }
- String padString(String value, int spaces) {
- if(value.length() >= spaces) return value; // If there are already enough characters, simply return what was given
- for(int i = value.length(); i < spaces; i ++) //Cycle through all of the missing spaces...
- value += " "; //...and add a space
-
- return value; //Return the formatted String
- }
That example should now work (unless I have made some big error somewhere along the line). You have already seen that
println() works on its own, however, so I thought it would be a good idea to explain...
println() is
exactly the same thing as
System.out.println(). Processing provides a shorthand way to say that.
When you call either of those functions, it will write to the default output file. This is also where errors are written. It is used primarily for simple debugging. In any decent Java editor (including Processing), the console (which you describe as showing up on the screen) will display all of the text written to the default output file.
As you can see, C is probably better designed for these purposes, but it is not at all impossible to do with Processing. Hopefully this (incredibly) long response will help you get started with Processing.
Further Reading:
createWriter() Reference
PrintWriter Reference
println() Reference
Objects Tutorial