How do I prevent the [] to appear (using strings)?

So I'm trying to get some keyboard input. Right now, I know that the code to delete the last letter from a string is:

if (key == BACKSPACE) 
  {
  written=written.substring(0,written.length()-1);
  }

But I'm showing what's being typed in the window. When I press BACKSPACE, in the window appears the [] symbol. I don't want it to appear and, instead, I want it to be replaced by the new letter the user types.

How to?

Answers

  • Can't figure out any problems from what you've posted so far.
    btW, a working online example: (*)

    http://studio.processingtogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR/latest

  • yes, please post your entire code

    btw

    you should say

    written = written + key;

    only when it's not backspace

    if (key == BACKSPACE)
      {
      written=written.substring(0,written.length()-1);
      }
    else {
       written = written + key;
    }
    

    Greetings!

  • here....

    // 
    
    String written = "";
    
    void draw () {
    } // func 
    
    //
    
    void keyPressed() {
      //
      if (key == BACKSPACE)
      {
        if (written.length()>0)
          written = written.substring(0, written.length()-1);
        // println (  key + "  -  "  + int (key));
      }
      else {
        written = written + key;
      }
      println ( written );
      //
    }
    //
    
  • edited March 2014

    Sorry. Well, basically my whole code is this (variables are in spanish):

    String[] adjetivos={"bonita","fea","hermosa","fantasmal","kawaii","sucia","inteligente","pendeja"};
    String adj="... ";
    int adj2=0;
    String nombre="";
    String escrito="";
    Boolean input=true;
    Boolean todavia=true;
    
    void setup()
    {
      size(800,600);
      background(0);
    
      textSize(20);
      textAlign(CENTER);
    }
    
    void keyPressed()
    {
      if (key == '\n')
      {
        nombre=escrito;
        input=false;
    
        int adjR=int(random(adjetivos.length));
    
        if (adjR == adj2)
         {
           adjR=int(random(adjetivos.length));
           adj2=adjR;
         }
         adj=adjetivos[adjR]; //adjetivos[int(random(adjetivos.length))];
         println(adj);
         }
         else
         {
           if (input)
           {
           escrito+=key;
           }
         }
    }
    
    void draw()
    {
      background(0);
    
      if (input)
      {
        text("Escribe tu nombre: ",width/2,height/2-100);
        text(escrito,width/2,height/2);
      }
    
      if (!input)
      {
      text("Eres muy "+adj+", "+nombre,width/2,height/2);
      }
    }
    

    What I'm trying to do is that when hitting BACKSPACE, it removes the last letter. Like normally we use it. That's all. I know how to delete the last letter from a string (using subtstring), but I don't want to see the [] symbol appear. Try it out.

  • edited March 2014

    It's somewhat useless posting your code when the bugged part isn't included there! :-w
    Besides, there are some indentation inconsistencies. Better use CTRL+T inside Processing's IDE! :-\"

    Anyways, @Chrisir has already posted a snippet example on how to do it! And I've posted an online running example too!

    http://studio.processingtogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR/latest

    Why don't try to adapt the working algorithms to your own program? :>

  • edited March 2014 Answer ✓

    Your code doesn't use BACKSPACE, that's why you see the rectangle, showing an interpretation of the backspace char...
    Likewise, don't use '\n', use ENTER instead.

Sign In or Register to comment.