Help with text file to single letters needed

Hi, I need some help from experienced users…

I have 3 txt files: test1.txt, test2.txt and test3.txt
every txt file is made of 200 random letters with no spaces like: AAIOEELLEEIIAAOOWECERT etc…

I need to

  1. chose randomly one of the txt files
  2. get the single letters from the file
  3. make some if statements like:
    if (char x == ‘A’ ){ fill (200,0,0);}
    if (char x == ‘C’ ){ fill (200,0,200);}
    so that I can assign a color fill to a rect() object, whenever a letter has ben read by the software.

Thank you!

26 colors in a (3*) 200 random sequence? for one rectangle
what is the use of this?

and how did you do that 3 text files?
do they contain line feeds?
you typed them or you used processing?

anyhow start:
-a- read one text file and print it to console.
-b- read all 3 in the way
-c- that you have a array of 3 strings each 200 chars long
-d- make a selector variable by random to create int j = 0 or 1 or 2
-e- make a for loop k over 200 and select one character
of that selected string variable
-f- make a color array 26 colors long “colorarray”
-g- make a function what converts “A” … “Z” to 0 … 25 “col_num”
-h- select a color by the result of that function
-i- and make a

fill(colorarray[ col_num( textfilestring[j].charAt(k) ) ] );

but that all is not clear what kind of program flow you want to do?
just blink 200 times ( for 3.3 sec ) makes no sense?

what is the idea? press a key and get the next color?
write a timer to make that 3.3 sec to ?200 sec? ( one sec for each color? )

no user could see what file is selected,
( they all just random blinking )
unless you use that for different features? like rect size or position.

pls post your code using

</> code formatter 

when you have questions to above list of steps.

thank you.
I need this just as a starting basis, then I can program the rest.
I don’t need all 26 letters, the files will use just 6 or 7 letters, I’ve made an example here with 4.
I’ve managed to get the letters from the first line of the txt file and print them one at time on the monitor… But for some reasons the fill commands do not work.
Also I must understand how to load randomly a file.

void setup(){
  size (500,500);


void draw(){

String[] lines = loadStrings("test.txt");
for (int i = 0 ; i < lines.length; i++) {
String str = (lines[i]);
  for (int x = 0 ; x < str.length(); x++) {
  char test = str.charAt(x);
  switch (test){
    case 'a': 
    case 'b': 
    case 'c':
    case 'd':

— no delay in draw
— no file load in draw

you jump from step -a- to step -e-
pls stick with the plan.

and you should not have LF CR in the files!
so you need lines[0] only.

String fna = "data/test";
String fnb = ".txt";
String[] fnstring = {"","",""};

void setup() {
  size(500, 500);
  for ( int i = 0; i<3; i++) {
    String filename = fna+i+fnb;
    String[] lines = loadStrings(filename);
    println("load "+filename);
  for ( int i = 0; i<3; i++) println("i "+i+" : "+fnstring[i]);

for step -a- and -b- and -c-

step -d-
now make the random “j”

1 Like

Ok I’ve done it and it works, but now I have a very odd problem in the setup.
If I use a random(3) with 3 files in the data folder (file0.txt,file1.txt,file2.txt) it works.
BUT if I use a random(4) with 4 files in the data folder (file0.txt,file1.txt,file2.txt,file3.txt) it does not work and says ArrayIndexOutOfBoundsException: 3
Which is absurd because for now the files are totally identical and named correctly
I’m sorry to bother you again but this problem is really without logic.

String fna = "data/file";
String fnb = ".txt";
String[] fnstring = {"","",""};
int j;
float r;
float g;
float b;
float x= 0;
float y= 0;

void setup() {
    size(251, 482);
    j= (int)(random(4));
    print (j);
    String filename = fna+j+fnb;
    String[] lines = loadStrings(filename);
    println("load "+filename);


is the problem, right?


String[] fnstring = {"","",""};


oh gosh yes… how stupid I am.

hi, sorry, but that was not the idea.
as the 3 files are only 200 char long each, i recommended
to read them all / into a array of 3 strings /
( and provided the code )
so you can switch later between them with the new variable “j”

i think the problem
int( random(4) ) you found already,

int (random(3) ) 

allows 0 , 1 , 2 ( and not 3 )

int j=0;
boolean diagp = true;  

int assign_j() {
  int rj = int(random(0, 3));                 // rem: 3.0 is excluded
  if (diagp ) println("select j "+rj);
  return rj;

// pls note that new variable for good diagnostic while developing, and later easily can reduce the flooding of the console.

now for the character selection also a extra function pls.
so you can easy loop over the 200 chars in the
( here sorry, i not fixed a name for it in above list )

char get_one_char( int j, int k) {
  String thestring = fnstring[j];
  char thechar = '&';   // A .. Z 26 char (int 65 .. 90)   / & is 27 (int 38)
  if ( k < thestring.length() ) thechar = thestring.charAt(k);
  if (diagp ) print("get j "+j+" k "+k+" thechar "+thechar+" int "+int(thechar) );
  return thechar;

you understand that you need to make sure that you need to check if there is a character at that place,
the only question remains? what we do when the string is too short?
i replaced it with a dummy, so you can later decide what to do with it
but not have to worry about going out of range ( 200 ).

for a test you can loop that function already from draw()

// now need the colorarray:

color[] colorarray;
color c;

void make_color_array() {  // to be  called from setup
// ??? 

1 Like