Asking for user input and saving it to .txt
in
Programming Questions
•
2 years ago
Hi Everyone,
I am working on a simple program. Basically, the program is supposed to do the following:
A) Fetch co-ordinates for circles to be displayed on the screen from .txt file. (Done, see code below)
B) Display circles (Done, see code below)
C) Ask for user input (??)
D) Store user input in .txt file next to a value for the number of circles displayed (??) So, if there are 10 circles displayed, and the user types 12, the in the text file, the following should be displayed 12,10.
How can I amend the code below, to also include C and D?
/*
*** Our text file will look something like below, #
where each line corresponds to the x,y,z coordinate of a point!
coordX, coordY, coorzZ
coordX, coordY, coorzZ
coordX, coordY, coorzZ
... etc
*/
/*-----------------------------------------------------------------------------
*** GLOBAL VARIABLES ***
-----------------------------------------------------------------------------*/
ArrayList pointList; // arraylist to store the points in
/*-----------------------------------------------------------------------------
*** GLOBAL INIT ***
-----------------------------------------------------------------------------*/
void setup(){
//-------------------------------- GENERAL
size(800,800);
background(0);
//-------------------------------- POINTS
pointList = new ArrayList(); // instantiate an ArrayList
importTextFile(); // call our custom function
}
/*-----------------------------------------------------------------------------
*** MAIN PROGRAM LOOP ***
-----------------------------------------------------------------------------*/
void draw(){
//-------------------------------- GENERAL
background(0);
stroke(210);
fill(210,90);
//-------------------------------- VISUALIZE THE POINT SET
for(int i = 0; i < pointList.size(); ++i){
PVector V = (PVector) pointList.get(i);
ellipse(V.x,V.y, 20, 20);
}
}
/*-----------------------------------------------------------------------------
*** CUSTOM IMPORT FUNCTION ***
-----------------------------------------------------------------------------*/
void importTextFile(){
String[] strLines = loadStrings("points_16_9.txt"); // the name and extension of the file to import!
for(int i = 0; i < strLines.length; ++i){
String[] arrTokens = split(strLines[i], ','); // use the split array with character to isolate each component
float xx = float(arrTokens[0]); // cast string value to a float values!
float yy = float(arrTokens[1]); // cast string value to a float values!
pointList.add( new PVector(xx,yy) ); // add values to a new array slot
}
}
I am working on a simple program. Basically, the program is supposed to do the following:
A) Fetch co-ordinates for circles to be displayed on the screen from .txt file. (Done, see code below)
B) Display circles (Done, see code below)
C) Ask for user input (??)
D) Store user input in .txt file next to a value for the number of circles displayed (??) So, if there are 10 circles displayed, and the user types 12, the in the text file, the following should be displayed 12,10.
How can I amend the code below, to also include C and D?
/*
*** Our text file will look something like below, #
where each line corresponds to the x,y,z coordinate of a point!
coordX, coordY, coorzZ
coordX, coordY, coorzZ
coordX, coordY, coorzZ
... etc
*/
/*-----------------------------------------------------------------------------
*** GLOBAL VARIABLES ***
-----------------------------------------------------------------------------*/
ArrayList pointList; // arraylist to store the points in
/*-----------------------------------------------------------------------------
*** GLOBAL INIT ***
-----------------------------------------------------------------------------*/
void setup(){
//-------------------------------- GENERAL
size(800,800);
background(0);
//-------------------------------- POINTS
pointList = new ArrayList(); // instantiate an ArrayList
importTextFile(); // call our custom function
}
/*-----------------------------------------------------------------------------
*** MAIN PROGRAM LOOP ***
-----------------------------------------------------------------------------*/
void draw(){
//-------------------------------- GENERAL
background(0);
stroke(210);
fill(210,90);
//-------------------------------- VISUALIZE THE POINT SET
for(int i = 0; i < pointList.size(); ++i){
PVector V = (PVector) pointList.get(i);
ellipse(V.x,V.y, 20, 20);
}
}
/*-----------------------------------------------------------------------------
*** CUSTOM IMPORT FUNCTION ***
-----------------------------------------------------------------------------*/
void importTextFile(){
String[] strLines = loadStrings("points_16_9.txt"); // the name and extension of the file to import!
for(int i = 0; i < strLines.length; ++i){
String[] arrTokens = split(strLines[i], ','); // use the split array with character to isolate each component
float xx = float(arrTokens[0]); // cast string value to a float values!
float yy = float(arrTokens[1]); // cast string value to a float values!
pointList.add( new PVector(xx,yy) ); // add values to a new array slot
}
}
1