Read the values of variables from a TXT file in Processing











up vote
0
down vote

favorite












I have the following program in Proccessing and I try to find a way to read the values ​​of variables from a TXT file.



static final int ribbon_length = 255, H = 200; 

void setup() {
size(ribbon_length, H); //Διαστάσεις παλέτας
}

void draw() {
float p = 1;
int up_y = 10;
int widthh = 1;
int height = 180;
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(widthh*step, up_y, widthh, height);
noStroke();
}
}


The values tha I want to read from the txt is



  float p = 1; 
int up_y = 10;
int widthh = 1;
int height = 180;


I found BufferedReader command but I'm not sure if it is thaτ I want. I try ρθν an example..but it didn't work...



BufferedReader reader;
String line;
static final int ribbon_length = 255, H = 200;

void setup() {
size(ribbon_length, H);
reader = createReader("positions.txt");
}
.
.
.
.


Any ideas????



EDIT:
Thanks for your answers.
I try some changes after your commends. But It didn't show any color.



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();
}
}









share|improve this question
























  • Are you sure the file positions.txt exists? Are you sure the file positions.txt is in the same directory where the sketch .pde file is? Also it didn't work......what exactly happened?
    – Nicolás Carlo
    Feb 16 '14 at 23:02

















up vote
0
down vote

favorite












I have the following program in Proccessing and I try to find a way to read the values ​​of variables from a TXT file.



static final int ribbon_length = 255, H = 200; 

void setup() {
size(ribbon_length, H); //Διαστάσεις παλέτας
}

void draw() {
float p = 1;
int up_y = 10;
int widthh = 1;
int height = 180;
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(widthh*step, up_y, widthh, height);
noStroke();
}
}


The values tha I want to read from the txt is



  float p = 1; 
int up_y = 10;
int widthh = 1;
int height = 180;


I found BufferedReader command but I'm not sure if it is thaτ I want. I try ρθν an example..but it didn't work...



BufferedReader reader;
String line;
static final int ribbon_length = 255, H = 200;

void setup() {
size(ribbon_length, H);
reader = createReader("positions.txt");
}
.
.
.
.


Any ideas????



EDIT:
Thanks for your answers.
I try some changes after your commends. But It didn't show any color.



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();
}
}









share|improve this question
























  • Are you sure the file positions.txt exists? Are you sure the file positions.txt is in the same directory where the sketch .pde file is? Also it didn't work......what exactly happened?
    – Nicolás Carlo
    Feb 16 '14 at 23:02















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have the following program in Proccessing and I try to find a way to read the values ​​of variables from a TXT file.



static final int ribbon_length = 255, H = 200; 

void setup() {
size(ribbon_length, H); //Διαστάσεις παλέτας
}

void draw() {
float p = 1;
int up_y = 10;
int widthh = 1;
int height = 180;
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(widthh*step, up_y, widthh, height);
noStroke();
}
}


The values tha I want to read from the txt is



  float p = 1; 
int up_y = 10;
int widthh = 1;
int height = 180;


I found BufferedReader command but I'm not sure if it is thaτ I want. I try ρθν an example..but it didn't work...



BufferedReader reader;
String line;
static final int ribbon_length = 255, H = 200;

void setup() {
size(ribbon_length, H);
reader = createReader("positions.txt");
}
.
.
.
.


Any ideas????



EDIT:
Thanks for your answers.
I try some changes after your commends. But It didn't show any color.



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();
}
}









share|improve this question















I have the following program in Proccessing and I try to find a way to read the values ​​of variables from a TXT file.



static final int ribbon_length = 255, H = 200; 

void setup() {
size(ribbon_length, H); //Διαστάσεις παλέτας
}

void draw() {
float p = 1;
int up_y = 10;
int widthh = 1;
int height = 180;
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(widthh*step, up_y, widthh, height);
noStroke();
}
}


The values tha I want to read from the txt is



  float p = 1; 
int up_y = 10;
int widthh = 1;
int height = 180;


I found BufferedReader command but I'm not sure if it is thaτ I want. I try ρθν an example..but it didn't work...



BufferedReader reader;
String line;
static final int ribbon_length = 255, H = 200;

void setup() {
size(ribbon_length, H);
reader = createReader("positions.txt");
}
.
.
.
.


Any ideas????



EDIT:
Thanks for your answers.
I try some changes after your commends. But It didn't show any color.



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();
}
}






java processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 6:30









Cœur

17k9102140




17k9102140










asked Feb 16 '14 at 18:27









Vassilis

122211




122211












  • Are you sure the file positions.txt exists? Are you sure the file positions.txt is in the same directory where the sketch .pde file is? Also it didn't work......what exactly happened?
    – Nicolás Carlo
    Feb 16 '14 at 23:02




















  • Are you sure the file positions.txt exists? Are you sure the file positions.txt is in the same directory where the sketch .pde file is? Also it didn't work......what exactly happened?
    – Nicolás Carlo
    Feb 16 '14 at 23:02


















Are you sure the file positions.txt exists? Are you sure the file positions.txt is in the same directory where the sketch .pde file is? Also it didn't work......what exactly happened?
– Nicolás Carlo
Feb 16 '14 at 23:02






Are you sure the file positions.txt exists? Are you sure the file positions.txt is in the same directory where the sketch .pde file is? Also it didn't work......what exactly happened?
– Nicolás Carlo
Feb 16 '14 at 23:02














2 Answers
2






active

oldest

votes

















up vote
1
down vote













You can have a plain txt where each of the lines is a variable, so you can use the BufferedReader like this.



BufferedReader br = new BufferedReader(new FileReader("fileName.txt"));


Then you read each line with br.readLine() and then you finally assign the read line to the variable, transforming it to the necessary type. For example, if your line is an int you would have to do



int myInt = Integer.parseInt(br.readLine())



You can also have a properties file, in which every line has the format "key = value". You can try this webpage to load that properties file: http://www.mkyong.com/java/java-properties-file-examples/ (check part 2: Loading a properties file)






share|improve this answer





















  • I hope you don't mind, but I added a processing-specific answer, as yours uses pure java only.
    – kevinsa5
    Feb 17 '14 at 22:46


















up vote
1
down vote













First: it's a bad idea to name variables width and height because these are special variables built into Processing. You should use something different, like wide and high, perhaps.



The simplest way to do it is to have a bare text file like this:



1
10
1
180


exactly as I have written it. You can then use the built-in functions of Processing to read in these values. These are easier alternatives to what xp500's answer provides, which is the correct answer for pure java.



void setup(){
....
String lines = loadStrings("positions.txt");
float p = float(lines[0]);
int up_y = int(lines[1]);
int wide = int(lines[2]);
int high = int(lines[3]);
....
}


This will work, but is not very maintainable because there are lots of hard-coded values. What if there's an extra empty line at the top of the text file? The first line (lines[0]) won't have p's value, and the second line (lines[1]) will have p's values instead of up_y's! This is just something to keep in mind. If you end up having other things in your text file (such as variable names), you could use split() to get the number out of the string. Example:



text file:



p=1
up_y=10


processing code:



String lines = loadStrings("input.txt")
float p = float(split(lines[0], "=")[1]); // split on the "=" and take the second element
int up_y = int(split(lines[1], "=")[1]);


If you choose this approach, you might even check for the variable name in the line before assigning it, just in case the file is jumbled. Something like if(!lines[0].contains("p")) print("error!");






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21815517%2fread-the-values-of-variables-from-a-txt-file-in-processing%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You can have a plain txt where each of the lines is a variable, so you can use the BufferedReader like this.



    BufferedReader br = new BufferedReader(new FileReader("fileName.txt"));


    Then you read each line with br.readLine() and then you finally assign the read line to the variable, transforming it to the necessary type. For example, if your line is an int you would have to do



    int myInt = Integer.parseInt(br.readLine())



    You can also have a properties file, in which every line has the format "key = value". You can try this webpage to load that properties file: http://www.mkyong.com/java/java-properties-file-examples/ (check part 2: Loading a properties file)






    share|improve this answer





















    • I hope you don't mind, but I added a processing-specific answer, as yours uses pure java only.
      – kevinsa5
      Feb 17 '14 at 22:46















    up vote
    1
    down vote













    You can have a plain txt where each of the lines is a variable, so you can use the BufferedReader like this.



    BufferedReader br = new BufferedReader(new FileReader("fileName.txt"));


    Then you read each line with br.readLine() and then you finally assign the read line to the variable, transforming it to the necessary type. For example, if your line is an int you would have to do



    int myInt = Integer.parseInt(br.readLine())



    You can also have a properties file, in which every line has the format "key = value". You can try this webpage to load that properties file: http://www.mkyong.com/java/java-properties-file-examples/ (check part 2: Loading a properties file)






    share|improve this answer





















    • I hope you don't mind, but I added a processing-specific answer, as yours uses pure java only.
      – kevinsa5
      Feb 17 '14 at 22:46













    up vote
    1
    down vote










    up vote
    1
    down vote









    You can have a plain txt where each of the lines is a variable, so you can use the BufferedReader like this.



    BufferedReader br = new BufferedReader(new FileReader("fileName.txt"));


    Then you read each line with br.readLine() and then you finally assign the read line to the variable, transforming it to the necessary type. For example, if your line is an int you would have to do



    int myInt = Integer.parseInt(br.readLine())



    You can also have a properties file, in which every line has the format "key = value". You can try this webpage to load that properties file: http://www.mkyong.com/java/java-properties-file-examples/ (check part 2: Loading a properties file)






    share|improve this answer












    You can have a plain txt where each of the lines is a variable, so you can use the BufferedReader like this.



    BufferedReader br = new BufferedReader(new FileReader("fileName.txt"));


    Then you read each line with br.readLine() and then you finally assign the read line to the variable, transforming it to the necessary type. For example, if your line is an int you would have to do



    int myInt = Integer.parseInt(br.readLine())



    You can also have a properties file, in which every line has the format "key = value". You can try this webpage to load that properties file: http://www.mkyong.com/java/java-properties-file-examples/ (check part 2: Loading a properties file)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 16 '14 at 18:35









    xp500

    1,306410




    1,306410












    • I hope you don't mind, but I added a processing-specific answer, as yours uses pure java only.
      – kevinsa5
      Feb 17 '14 at 22:46


















    • I hope you don't mind, but I added a processing-specific answer, as yours uses pure java only.
      – kevinsa5
      Feb 17 '14 at 22:46
















    I hope you don't mind, but I added a processing-specific answer, as yours uses pure java only.
    – kevinsa5
    Feb 17 '14 at 22:46




    I hope you don't mind, but I added a processing-specific answer, as yours uses pure java only.
    – kevinsa5
    Feb 17 '14 at 22:46












    up vote
    1
    down vote













    First: it's a bad idea to name variables width and height because these are special variables built into Processing. You should use something different, like wide and high, perhaps.



    The simplest way to do it is to have a bare text file like this:



    1
    10
    1
    180


    exactly as I have written it. You can then use the built-in functions of Processing to read in these values. These are easier alternatives to what xp500's answer provides, which is the correct answer for pure java.



    void setup(){
    ....
    String lines = loadStrings("positions.txt");
    float p = float(lines[0]);
    int up_y = int(lines[1]);
    int wide = int(lines[2]);
    int high = int(lines[3]);
    ....
    }


    This will work, but is not very maintainable because there are lots of hard-coded values. What if there's an extra empty line at the top of the text file? The first line (lines[0]) won't have p's value, and the second line (lines[1]) will have p's values instead of up_y's! This is just something to keep in mind. If you end up having other things in your text file (such as variable names), you could use split() to get the number out of the string. Example:



    text file:



    p=1
    up_y=10


    processing code:



    String lines = loadStrings("input.txt")
    float p = float(split(lines[0], "=")[1]); // split on the "=" and take the second element
    int up_y = int(split(lines[1], "=")[1]);


    If you choose this approach, you might even check for the variable name in the line before assigning it, just in case the file is jumbled. Something like if(!lines[0].contains("p")) print("error!");






    share|improve this answer

























      up vote
      1
      down vote













      First: it's a bad idea to name variables width and height because these are special variables built into Processing. You should use something different, like wide and high, perhaps.



      The simplest way to do it is to have a bare text file like this:



      1
      10
      1
      180


      exactly as I have written it. You can then use the built-in functions of Processing to read in these values. These are easier alternatives to what xp500's answer provides, which is the correct answer for pure java.



      void setup(){
      ....
      String lines = loadStrings("positions.txt");
      float p = float(lines[0]);
      int up_y = int(lines[1]);
      int wide = int(lines[2]);
      int high = int(lines[3]);
      ....
      }


      This will work, but is not very maintainable because there are lots of hard-coded values. What if there's an extra empty line at the top of the text file? The first line (lines[0]) won't have p's value, and the second line (lines[1]) will have p's values instead of up_y's! This is just something to keep in mind. If you end up having other things in your text file (such as variable names), you could use split() to get the number out of the string. Example:



      text file:



      p=1
      up_y=10


      processing code:



      String lines = loadStrings("input.txt")
      float p = float(split(lines[0], "=")[1]); // split on the "=" and take the second element
      int up_y = int(split(lines[1], "=")[1]);


      If you choose this approach, you might even check for the variable name in the line before assigning it, just in case the file is jumbled. Something like if(!lines[0].contains("p")) print("error!");






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        First: it's a bad idea to name variables width and height because these are special variables built into Processing. You should use something different, like wide and high, perhaps.



        The simplest way to do it is to have a bare text file like this:



        1
        10
        1
        180


        exactly as I have written it. You can then use the built-in functions of Processing to read in these values. These are easier alternatives to what xp500's answer provides, which is the correct answer for pure java.



        void setup(){
        ....
        String lines = loadStrings("positions.txt");
        float p = float(lines[0]);
        int up_y = int(lines[1]);
        int wide = int(lines[2]);
        int high = int(lines[3]);
        ....
        }


        This will work, but is not very maintainable because there are lots of hard-coded values. What if there's an extra empty line at the top of the text file? The first line (lines[0]) won't have p's value, and the second line (lines[1]) will have p's values instead of up_y's! This is just something to keep in mind. If you end up having other things in your text file (such as variable names), you could use split() to get the number out of the string. Example:



        text file:



        p=1
        up_y=10


        processing code:



        String lines = loadStrings("input.txt")
        float p = float(split(lines[0], "=")[1]); // split on the "=" and take the second element
        int up_y = int(split(lines[1], "=")[1]);


        If you choose this approach, you might even check for the variable name in the line before assigning it, just in case the file is jumbled. Something like if(!lines[0].contains("p")) print("error!");






        share|improve this answer












        First: it's a bad idea to name variables width and height because these are special variables built into Processing. You should use something different, like wide and high, perhaps.



        The simplest way to do it is to have a bare text file like this:



        1
        10
        1
        180


        exactly as I have written it. You can then use the built-in functions of Processing to read in these values. These are easier alternatives to what xp500's answer provides, which is the correct answer for pure java.



        void setup(){
        ....
        String lines = loadStrings("positions.txt");
        float p = float(lines[0]);
        int up_y = int(lines[1]);
        int wide = int(lines[2]);
        int high = int(lines[3]);
        ....
        }


        This will work, but is not very maintainable because there are lots of hard-coded values. What if there's an extra empty line at the top of the text file? The first line (lines[0]) won't have p's value, and the second line (lines[1]) will have p's values instead of up_y's! This is just something to keep in mind. If you end up having other things in your text file (such as variable names), you could use split() to get the number out of the string. Example:



        text file:



        p=1
        up_y=10


        processing code:



        String lines = loadStrings("input.txt")
        float p = float(split(lines[0], "=")[1]); // split on the "=" and take the second element
        int up_y = int(split(lines[1], "=")[1]);


        If you choose this approach, you might even check for the variable name in the line before assigning it, just in case the file is jumbled. Something like if(!lines[0].contains("p")) print("error!");







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 17 '14 at 22:45









        kevinsa5

        2,41911925




        2,41911925






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21815517%2fread-the-values-of-variables-from-a-txt-file-in-processing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine