Hello processing foundation.
I want to remove the first character from a string when the key pressed is equal to the first character. For example if the string word is “hello”, when I want the “h” in the word to be removed when the key pressed = ‘h’, and afterwards the “e” when key pressed = “e” and so on-
I know that I will need to create a for-loop and use substring(), but I do not how to to set this up.
This is the code i have for now:
String text="";
String[] word = { "hello", "welcome", "how", "are" }; //Just random words for testing
int randomWord;
void setup() {
size(100, 100);
frameRate(0.5);
}
void draw() {
background(180);
randomWord = int(random(word.length));
text(word[randomWord], 10, 10);
int letters = word[randomWord].length(); // the number of letters there is in the random word
println(letters);
for( int n=0; ??
//Remove first letter of word when key pressed = first letter of the random word
Instead of using a for loop, what I did was on every key press check if the key equals the first letter (more efficient than using a for loop) (a key thing here is using String.equals() instead of ‘==’). I also made a check so it only deletes the first letter if text.length is greater than 0 (so it doesn’t give an error when we press a key while the entire text is deleted).
And it seems like you wanted the word to change every two seconds, looking at what you did with frameRate(), instead of using frameRate() I made a simple int timer that goes up every frame and when it reaches a certain point, something happens and it goes back to 0 making a loop. You can control when the word changes with the seconds variable.
@qewer3322 thank you very much, this works exactly as intended:)
But if I on the other hand want the random word to change when you write the correct letters on the keyboard then how do you do this?
I know that I can use this code:
int index;
if (text1.equals(word[index])) {
indeks = int(random(word.length));
text1="";
}
This code checks if the text typed matches the random word, but the problem I have with this is that you cannot type anything wrong. Therefore I want the word to change if all letters in the word have been typed/pressed in the correct order. For example if you need to type “hello” then I want the word to change if you for example type “hqeqlqlqo” or “heeeello”.
(note that i do not want to use the backspace to delete a wrong letter typed)