Console-based Input -- Counter Implementation

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.

i think that might be possible, but you must “talk” with the user what input is required next

or you make up some code at time of input

void keyPressed() {
  if ( keyCode == ENTER ) {
    send = trim(in);   // store
    in = "";     // reset
    if ( diagp ) println("ENTER "+send);
    String[] sendsplit = split(send, '=');
    if ( diagp ) println(sendsplit);
    
    if      ( sendsplit[0].equals("a") ) { // parameter a
      name_a = sendsplit[1];
      println("name "+name_a);
    } else if ( sendsplit[0].equals("b") ) { // parameter b
      int_b = int(sendsplit[1]);
      println("int "+int_b);
    } else if ( sendsplit[0].equals("c") ) { // parameter c
      list_c = split(sendsplit[1], ",");
      println("list ");
      printArray(list_c);
    } else println("not see a=name or b=999 or c=a,b,c,d,e ");
  } 
  in += key;
  if ( diagp ) println(in);
}


so the user define what value to enter / change

but i think all that is ugly,
find a way to give/enter all that in a ONE LINE ?
possibly syntax would be CSV or JSON


why all the things from last topic like
small input / edit windows in canvas for this would not work in your project?

Using the keypressed() FUNCTION that is not the case

Show your code

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

}

This is the second tab:

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