Problem with storing user-entered text in a string

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:

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:

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

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

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

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:

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

key != CODED
3 Likes

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) that character will be eliminated. Give it a try :slight_smile:

1 Like

Must be if (…

Chrisir

1 Like

Basically before that check if(key!=CODED)

2 Likes

This solved it! Thank you so much!

1 Like

This was the answer! Thank you!!!

2 Likes

here is an example



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 "";
}
//
1 Like