I’m attempting to write a program in which the user must input three variables. The first is a string which simply stores a name. The second is a string which gets converted to an integer using parseInt. The third is a string array with five objects.
All are being done in CONSOLE mode and console mode only. No mouse input, only keyboard.
For all, text is entered with the enter/return key. This is also what triggers variable storage.
All three work on their own. Each is an individual function to populate a class with three variables. (String, int, string array). However, when worked in succession, the return key seems to trigger the others.
The solution seemed to be a counter, which would implement which function is run at each time. After data entry, this counter variable counts up with a ++ at the end of it.
It just doesn’t seem to work. How can I make it so that each function runs individually without overlap? I don’t want to see parts of another function get populated with the key input from the first one or start running early.
Here’s the code…I have this in two tabs. This is the first tab:
Customer myCustomer;
String[] products = new String[5]; //sets up storage array variable
String getProducts=""; //sets up input placeholder variable
String name=""; //stores customer name
String getName=""; //gets customer name
String getAge="";
int l = 0; //counter for
int age;
int counter = 0;
void setup() {
}
void draw() {} //again, nothing needed
void keyPressed(){ //this is when the key is pressed
if(counter==0){
custNamer();}
if(counter==1){
Namer();}
}
class Customer
{
Customer(String getName, int getAge, String[] getProducts)//defines constructor
{
}
}
void custNamer() {
if (counter == 0) {
if ( keyCode == ENTER ) { //if user presses enter, we do the following:
println("Customer Name: "+getName); //print out what they just typed in
name=getName; //store what they typed in one of the array slots
name = ""; //clears placeholder variable
getProducts = "";
counter++;
}
else getName += key; //any alphanumeric key can be used for input
}
}
void Namer(){ //this is when the key is pressed
if (counter == 1){
if ( keyCode == ENTER ) { //if user presses enter, we do the following:
println("Product Entered: "+name); //print out what they just typed in
products[l]=getProducts; //store what they typed in one of the array slots
getProducts = ""; //clears placeholder variable
l++; //increases counter by 1
if (l >= 5) {
println(" ");
println("Products purchased:");
println(" ");
printArray(products);
counter++;
}
}
else getProducts += key; //any alphanumeric key can be used for input
//
}
}
void NumberThing() {
if(keyCode == ENTER)
{
age = Integer.parseInt(getAge);
println ("Customer age is: " +age);
getAge = "";
}
else
getAge += key;
}