How to read from .txt file.

edited January 2015 in Questions about Code

Hey guys... Im trying to get this program to read 3 values from a .txt file, i have the file loaded as a string, but i need to split it so i can asign the values to floats.

code:

PrintWriter Values;

int red = 1;

int green = 2;

int blue = 3;

String[] readValues;

void setup(){

size(100,100);

readValues = loadStrings("Values.txt");

println(readValues);

String[] pieces = split(readValues[0], '\t');

Values = createWriter("Values.txt");

}

void draw(){

background(255);

}

void mousePressed(){

Values.println(red + "\t" + green + "\t" + blue + "\t");

Values.flush();

//exit();

}

Answers

  • Answer ✓

    Well, you're loading the string array with loadStrings() properly. And you're splitting the first string read into an array of strings using the split() function properly. But then you never do anything with those values! You could do it with three assignments...

    float r,g,b;
    String[] textFile = loadStrings("");
    String[] parts = split(textFile[0], '\t');
    r = float(parts[0]);
    g = float(parts[1]);
    // etc.
    
  • Thx :D its works

Sign In or Register to comment.