Loading...
Logo
Processing Forum
This is the example code:
Copy code
  1. grp = RG.getText("Hello world!", "FreeSans.ttf", 72, CENTER);
and I would like to replace "Hello world!" with text from a .txt file.

Is this possible?

Replies(3)

thanks for the answer,

I've tried it already and I understand, that loadStrings(); would be the perfect solution.
But when I try to use it with text(); or  RG.getText(); it's not working.

this is a simple loadStrings code:

Copy code
  1. String[] txt = loadStrings("/Users/rjth/Desktop/data.txt");
  2. println("there are " + txt.length + " lines");
  3. println(txt);
when I add text(); to this, error message comes up:

Copy code
  1. String[] txt = loadStrings("/Users/rjth/Desktop/data.txt");
  2. println("there are " + txt.length + " lines");
  3. println(txt);
  4. text(txt, 1, 1);
error: The method text(char[], int, int float, float) in the type PApplet is not applicable for the arguments (String[], int, int)

maybe I misunderstood something completely fundamental, but I just don't get it.
what am I doing wrong?
Function loadStrings() gets us not a single whole String, but an Array of String -> String[].
Re-read its reference to grasp it better.

Now, if you wanna glue a String[] into 1 String; use function join().
This way, you can use it directly in text() w/o accessing each String element of an Array 1 by 1.