peculiar behavior w/ selectinput?
in
Programming Questions
•
1 year ago
The following code exhibits a behavior that doesn't make sense to me... perhaps someone can explain or perhaps a bug?
(running 1.5 in Windows 7.)
What I call "version 1" works OK. (you can select any .pde or .txt file when prompted and it runs fine.)
If I comment out "version 1" and uncomment "version 2" the pgm doesn't compile -- the mapData array isn't recognized.
So it would appear that within the "else" part of the if statement the mapData array is recognized but outside of it it isn't???
// version 1
void setup() {
String loadPath = selectInput("choose any .txt or .pde file"); // Opens file chooser
if (loadPath == null) {
println("No file was selected...");
} else {
String[] mapData = loadStrings(loadPath) ;
println("There are " + mapData.length + " lines") ; // this is how to get length of array
println("first line is " + mapData[0]) ;
} // in this version print statements are within else path
println("!! pgm exit !!!") ;
}
// end of version 1
// version 2
// void setup() {
// String loadPath = selectInput("choose map data file"); // Opens file chooser
// if (loadPath == null) {
// println("No file was selected...");
//} else {
// String[] mapData = loadStrings(loadPath) ;
//} // in this version following 2 statements are out of else path
// println("There are " + mapData.length + " lines") ; // this is how to get length of array
// println("first line is " + mapData[0]) ;
// println("!! pgm exit !!!") ;
// }
// end of version 2
(running 1.5 in Windows 7.)
What I call "version 1" works OK. (you can select any .pde or .txt file when prompted and it runs fine.)
If I comment out "version 1" and uncomment "version 2" the pgm doesn't compile -- the mapData array isn't recognized.
So it would appear that within the "else" part of the if statement the mapData array is recognized but outside of it it isn't???
// version 1
void setup() {
String loadPath = selectInput("choose any .txt or .pde file"); // Opens file chooser
if (loadPath == null) {
println("No file was selected...");
} else {
String[] mapData = loadStrings(loadPath) ;
println("There are " + mapData.length + " lines") ; // this is how to get length of array
println("first line is " + mapData[0]) ;
} // in this version print statements are within else path
println("!! pgm exit !!!") ;
}
// end of version 1
// version 2
// void setup() {
// String loadPath = selectInput("choose map data file"); // Opens file chooser
// if (loadPath == null) {
// println("No file was selected...");
//} else {
// String[] mapData = loadStrings(loadPath) ;
//} // in this version following 2 statements are out of else path
// println("There are " + mapData.length + " lines") ; // this is how to get length of array
// println("first line is " + mapData[0]) ;
// println("!! pgm exit !!!") ;
// }
// end of version 2
1