# Console-based Input -- Counter Implementation

**URL:** https://discourse.processing.org/t/console-based-input-counter-implementation/9948
**Category:** Coding Questions
**Created:** [April 4, 2019, 3:57am UTC](https://discourse.processing.org/t/console-based-input-counter-implementation/9948 "2019-04-04T03:57:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![clevelandbrowns](https://avatars.discourse-cdn.com/v4/letter/c/bbce88/32.png) [@clevelandbrowns](https://discourse.processing.org/u/clevelandbrowns)
#### Post date: [April 4, 2019, 3:57am UTC](https://discourse.processing.org/t/console-based-input-counter-implementation/9948/1 "2019-04-04T03:57:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [April 4, 2019, 5:21am UTC](https://discourse.processing.org/t/console-based-input-counter-implementation/9948/2 "2019-04-04T05:21:53Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 4, 2019, 6:19am UTC](https://discourse.processing.org/t/console-based-input-counter-implementation/9948/3 "2019-04-04T06:19:36Z")

</div>

> [@clevelandbrowns](#):
>
> However, when worked in succession, the return key seems to trigger the others.

Using the keypressed() FUNCTION that is not the case

Show your code

---

<div class="post-metadata">

### Author: ![clevelandbrowns](https://avatars.discourse-cdn.com/v4/letter/c/bbce88/32.png) [@clevelandbrowns](https://discourse.processing.org/u/clevelandbrowns)
#### Post date: [April 4, 2019, 12:45pm UTC](https://discourse.processing.org/t/console-based-input-counter-implementation/9948/4 "2019-04-04T12:45:34Z")

</div>

Here’s the code…I have this in two tabs. This is the first tab:

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

}

```

---

<div class="post-metadata">

### Author: ![clevelandbrowns](https://avatars.discourse-cdn.com/v4/letter/c/bbce88/32.png) [@clevelandbrowns](https://discourse.processing.org/u/clevelandbrowns)
#### Post date: [April 4, 2019, 12:46pm UTC](https://discourse.processing.org/t/console-based-input-counter-implementation/9948/5 "2019-04-04T12:46:09Z")

</div>

This is the second tab:

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

```
