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();
}
}
java processing
add a comment |
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();
}
}
java processing
Are you sure the filepositions.txt
exists? Are you sure the filepositions.txt
is in the same directory where the sketch.pde
file is? Alsoit didn't work...
...what exactly happened?
– Nicolás Carlo
Feb 16 '14 at 23:02
add a comment |
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();
}
}
java processing
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
java processing
edited Nov 11 at 6:30
Cœur
17k9102140
17k9102140
asked Feb 16 '14 at 18:27
Vassilis
122211
122211
Are you sure the filepositions.txt
exists? Are you sure the filepositions.txt
is in the same directory where the sketch.pde
file is? Alsoit didn't work...
...what exactly happened?
– Nicolás Carlo
Feb 16 '14 at 23:02
add a comment |
Are you sure the filepositions.txt
exists? Are you sure the filepositions.txt
is in the same directory where the sketch.pde
file is? Alsoit 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
add a comment |
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)
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
add a comment |
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!");
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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!");
add a comment |
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!");
add a comment |
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!");
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!");
answered Feb 17 '14 at 22:45
kevinsa5
2,41911925
2,41911925
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Are you sure the file
positions.txt
exists? Are you sure the filepositions.txt
is in the same directory where the sketch.pde
file is? Alsoit didn't work...
...what exactly happened?– Nicolás Carlo
Feb 16 '14 at 23:02