This site requires JavaScript, please enable it in your browser!
Greenfoot back
gusbus123
gusbus123 wrote ...

2012/7/30

Removing a letter from a String.

1
2
3
gusbus123 gusbus123

2012/7/30

#
Right now im working on a basic base scenario, and need some help with removing a letter from a given string. So say that i have a string created like this: private String word = "wateva"; I want to be able to press backspace and it will remove the last letter of it. Is there a way to do this?
erdelf erdelf

2012/7/30

#
untested
    
   StringBuffer buf = new StringBuffer(word.length() - 1 );
   buf.append( word.substring(0,word.length) ).append( word.substring(word.length+1) );
   word = buf.toString();
   System.out.println(word);
  
SPower SPower

2012/7/30

#
You can also just do this:
private String word = "wateva";
// some later:
String shortWord = word.substring(0, word.length() -2);
also untested: it could happen (if I'm not paying attention right now) that you remove 2 characters. But again, I'm not really paying attention here, I'm a bit ill :)
gusbus123 gusbus123

2012/7/31

#
ok thx btw what does the button "delete" return as? Whenever i press it the returned result is a double spacing... How would you be able to referance to the delete button???
SPower SPower

2012/7/31

#
What should the delete button actually do? I don't understand, sorry.
gusbus123 gusbus123

2012/7/31

#
it should do the same as the backspace. I just need to know what to reference in the code so i can accually use it. Right now i dont know what to put down when i need to use the delete button. As in how the backspace button is "backspace" i want to know what the delete button is represented as.
SPower SPower

2012/7/31

#
Create an instance variable in a subclass of World:
private Button deleteButton = *something*;
or initialize it somewhere else:
deleteButton = *something*;
in the act method of that class:
if (Greenfoot.mouseClicked(deleteButton)) {
     // the method for removing last character
}
gusbus123 gusbus123

2012/7/31

#
no i meant the litteral delete button on your keyboard. The one thats usually below the insert button. Right now when i press the delete button it returns with basically this " ". Although it has the size of 2 spacings its accually 1 character. I dont know how to use "getKey()" to know when the delete button is pressed. I can get the others but not the delete button.
SPower SPower

2012/7/31

#
This is how you use getKey:
String key = Greenfoot.getKey();
if (key != null) { // a key was pressed
     // do something with it
}
the great thing of getKey is this: imagine you press the key 'a'. The above code gets executed, and the String key is "a". The above code gets executed again, and the a key is still pressed, not released. Now, key will be null. This is quite useful for entering text. If you need help with a textfield, in my Save it scenario is a Textfield class, and a SecureTextfield (I think it's called that way :) ), which is for entering passwords. Maybe you can use that. EDIT: it's called PrivateTextField :)
gusbus123 gusbus123

2012/7/31

#
I know how to use getKey. Iv been using it for my own scenario that is just a somewhat typing program. Iv got it to do basically everything i want except i cant get what the delete key is supposed to do. This is because i dont know what to reference when im trying to make the getKey to know that the key i pressed is the delete key. right know when i press the delete key it returns as " " which has the same space as pressing the space button twice. But it is a single character and is completely different. I want to know what the delete button returns as so i can use it to act the same as the backspace button. I just cant find where that double spacing that the delete button produces is.
SPower SPower

2012/7/31

#
O, big sorry, I stopped reading after " I dont know how to use "getKey()" ". The backspace key on the keyboard is "Backspace", so do this:
if ("Backspace".equals(key)) {
    // remove last letter
}
gusbus123 gusbus123

2012/7/31

#
nono. The button i dont know is the delete button. iv tried "delete", " " (which is just two spaces). Both doesnt work.
SPower SPower

2012/7/31

#
O, now I understand. I'm sorry, I thought you were talking about the backspace key. Have you tried this:
String key = Greenfoot.getKey();
if (key != null)
  System.out.println(key+"\n");
and pressing the delete key?
danpost danpost

2012/7/31

#
You can check for the 'delete' button with the following:
String key = Greenfoot.getKey();
if (key.length() == 1 && (char) 127 == key.charAt(0)) {
EDIT: I guess if you want to call it something, call it "" + ((char) 127)
gusbus123 gusbus123

2012/7/31

#
the System.out thing didnt work. And i think what u were trying to do there was that if ANY button is pressed then it will do the print the extra words "\n". What that does right now is that it prints out the letters i pressed in that extra screen. I wanted to print it within the scenario (which i already have working) but the uneccesary buttons like delete, backspace, enter, control, escape and space will not add the extra word but do what it normally does on the keyboard. So backspace will remove a character, enter will enter the current complete string (wateva i already typed in), escape will do something like clear the screen, and delete will clear what you have typed. With what danpost has said, it says theres a "null pointer exeption" when i start running the program. This will probrably be easier if i upload this and give you the source to play around with, ill upload and give you guys the link in a sec. Ok heres the link: http://www.greenfoot.org/scenarios/5748
There are more replies on the next page.
1
2
3