read variable values from a txt file

//http://forum.processing.org/two/discussion/3077/how-to-read-the-values-​​of-variables-from-a-txt-file#Item_5

After the conversation and corrections of code I ended up in the following program. The program creates a simple color grading. Just trying to put the values ​​from TXT file but while the program does not make mistakes and opens the canvas does not make the picture. Displays a gray canvas.

static final int ribbon_length = 255, H = 200; 

void setup() {
  size(ribbon_length, H);
}

void draw() {
  String[] lines = loadStrings("input.txt");
  float p = float(split(lines[0], "=")[1]);
  int up_y = int(split(lines[1], "=")[1]);
  int wide = int(split(lines[2], "=")[1]);
  int high = int(split(lines[3], "=")[1]);
  float a = pow (ribbon_length, 1-p);
  float colour = 0;
  for (int step = 0; step <= 255; step++) { 
      colour = a * pow (step, p);
      fill(colour,0,0); 
      rect(wide*step, up_y, wide, high);
      noStroke();
   }
}
Tagged:

Answers

  • Break your problem up into smaller pieces. Use print statements to figure out what your file is reading in. Something like:

    String[] lines = loadStrings("input.txt");
    println("Line count: " + lines.length);
    String line0 = lines[0];
    println("Line 0: " + line0);
    String[] line0Split = split(lines[0], "=");
    println("Line 0 split length: " + line0Split.length);
    for(String s : line0Split){
       println(s);
    }
    

    Look at what it prints out and figure out whether it's making sense based on your expectations.

  • Well, first, don't read the file in draw() (or add noLoop() to setup()).

    Next, it depends on what you have in your file. As advised, print out the values to see what is going on.

    Note that int() can fail if there are spaces in the string, you might want to use trim().

  • edited February 2014

    it would be nice if you'd show the first five lines of your "input.txt", but see other thread (mentioned above) please

Sign In or Register to comment.