# Problem with storing user-entered text in a string

**URL:** <https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348>\
**Category:** Coding Questions\
**Created:** [August 19, 2020, 3:54am UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348 "2020-08-19T03:54:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![benvining](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@benvining](https://discourse.processing.org/u/benvining)\
**Post date:** [August 19, 2020, 3:54am UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348/1 "2020-08-19T03:54:23Z")

</div>

Hello all! As part of a larger project, I created a popup window where a user can enter some text (to change their username in a game). I have a String that stores the player’s username and a separate String that stores the entered text. I initialize my typingPlayerName string by setting it equal to “” , and I use the “typingPlayerName = typingPlayerName + key” method to capture the user’s typing. However, Processing keeps storing a null character at the start of the string. Using println to view the user’s entered text displays a strange box glyph character at the front of the user’s typed text.

I think it’s a problem with the way I’m initializing the typingPlayerName string by setting it equal to a null value, but how do you get around this? I need this string to be a global variable because the way I’m drawing the screens for this game, each area/screen is drawn by a different function.

I’ve tried using typingPlayerName.substring(1), and I’ve also creating a nullTestString and then saying:

```auto
String nullTestString = "";
if (typingPlayerName.equals(nullTestString) == true) {
   typingPlayerName = str(key);
} else {
   typingPlayerName = typingPlayerName + str(key);
}

```

but both of these approaches yield the exact same results – a strange glyph character at the front of the text and an error message in the console that says “No glyph found for the (\uFFFF) character”

Any ideas?

Here’s some sample code. This is part of a much larger project, this is just the keyPressed() function that does the capturing of input text:

```auto
boolean editingPlayerName == false;
String typingPlayerName = "";

void keyPressed() {
  if (editingPlayerName == true) {
    if (key == '\n') {
      playerName = typingPlayerName;
      typingPlayerName = "";
      updatePlayerName();
    } else if (key == BACKSPACE) {
      typingPlayerName = typingPlayerName.substring(0, max(0, typingPlayerName.length() - 1));
    } else {
      if(isTypingPlayerNameEmpty == true) {
         typingPlayerName = str(key);
      } else {
        typingPlayerName = typingPlayerName + str(key);
      }
      } 
    } 
}

void draw() {
if(editingPlayerName == true) {
    if(typingPlayerName.equals("") == true) {
      isTypingPlayerNameEmpty = true;
    } else {
      isTypingPlayerNameEmpty = false;
    }
  }
  if(editingPlayerName == false) {
    isTypingPlayerNameEmpty = true;
  }
}

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [August 19, 2020, 5:54am UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348/2 "2020-08-19T05:54:58Z")

</div>

I think I had a similar issue before, just take the substring of the string when it is required

---

<div class="post-metadata">

**Author:** ![benvining](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@benvining](https://discourse.processing.org/u/benvining)\
**Post date:** [August 19, 2020, 6:06am UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348/3 "2020-08-19T06:06:15Z")

</div>

I tried typingPlayerName.substring(1) and it still returns with the null character at the beginning of the text…

---

<div class="post-metadata">

**Author:** ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)\
**Post date:** [August 19, 2020, 6:38am UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348/4 "2020-08-19T06:38:27Z")

</div>

I think what happens here is (same thing happened to me). If you are pressing shift, capslock , enter etc. it also registeres those keys as a key and since they don’t have characters, they register as null. You’re probably pressing shift at the start of typing. What I didi to solve this was something like this:

```auto
if (text == true && key==BACKSPACE) {
    if (export.length()>0) {
      export=export.substring(0, export.length()-1);
    }
  } else if (text == true && key != CODED) {
    export+=key;
  }
}

```

So it registeres the key if the key is not coded. I hope this is what you are looking for. But now I realize this may not be what you are looking for but it is a useful thing to keep in mind. `:D`

```auto
key != CODED

```

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [August 19, 2020, 8:05am UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348/5 "2020-08-19T08:05:43Z")

</div>

Hi,

I tried using your sample code, but it is incomplete. Can you provide a more complete code?  
Also, please format your code using the \</\> button

Maybe using the trim() function ([https://processing.org/reference/trim\_.html](https://processing.org/reference/trim_.html)) that character will be eliminated. Give it a try 🙂

---

<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:** [August 19, 2020, 11:07am UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348/6 "2020-08-19T11:07:05Z")

</div>

> [@qewer3322](#):
>
> `f (text == true `

Must be if (…

Chrisir

---

<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:** [August 19, 2020, 11:09am UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348/7 "2020-08-19T11:09:00Z")

</div>

> [@benvining](#):
>
> typingPlayerName = typingPlayerName + str(key);

Basically before that check if(key!=CODED)

---

<div class="post-metadata">

**Author:** ![benvining](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@benvining](https://discourse.processing.org/u/benvining)\
**Post date:** [August 19, 2020, 12:24pm UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348/8 "2020-08-19T12:24:21Z")

</div>

This solved it! Thank you so much!

---

<div class="post-metadata">

**Author:** ![benvining](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@benvining](https://discourse.processing.org/u/benvining)\
**Post date:** [August 19, 2020, 12:24pm UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348/9 "2020-08-19T12:24:32Z")

</div>

This was the answer! Thank you!!!

---

<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:** [August 23, 2020, 2:58pm UTC](https://discourse.processing.org/t/problem-with-storing-user-entered-text-in-a-string/23348/10 "2020-08-23T14:58:59Z")

</div>

> [@MiguelSanches](#):
>
> Can you provide a more complete code?

here is an example

```auto

boolean boolEnteringText=true; // entering text yes/no
String export=""; // current text input
String result = ""; // list of text on the right  
boolean showBlinkingLine=true; // show blinking cursor yes/no

void setup() {
  size(800, 800);
  background(0);
}

void draw() {
  background(0);
  text(export + blinkingLine(), 22, 22);
  text(result, width-112, 22);
}

//---------------------------------------------------------------------------

void keyPressed() {
  if (boolEnteringText == true && key==BACKSPACE) {
    // delete last char
    if (export.length()>0) {
      export=export.substring(0, export.length()-1);
    }
  } else if (boolEnteringText == true && (key==RETURN||key==ENTER)) {
    // submit
    result+=export+"\n";
    export="";
    showBlinkingLine=true;
  }//else if
  else if (boolEnteringText == true && key != CODED) {
    // add char 
    export += key;
  }
}

String blinkingLine() {
  // toggle showBlinkingLine
  if (frameCount%17==0) 
    showBlinkingLine = ! showBlinkingLine;

  // return line or nothing     
  if (showBlinkingLine) 
    return "|";
  else return "";
}
//

```
