Hello Processing Foundation
I have this coding question, which I really hope someone will help me to solve:
This is my code so far:
String[] words = {"hello", "how", "helicopter", "who", "which", "that", "okay"}; //just some random words for showing purpose.
String CHAR = "-";
void setup() {
size (300, 300);
}
void draw() {
background(180);
textSize(30);
text(words[0], 10, 30);
text(words[1], 10, 70);
text(words[2], 10, 110);
text(words[3], 10, 150);
text(words[4], 10, 190);
text(words[5], 10, 230);
}
void keyTyped() {
replaceLetter();
redraw();
}
void replaceLetter() {
for (int i = 0; i < words.length; i++) {
int idx = min(words[i].lastIndexOf(CHAR) + 1, words[i].length() - 1);
char ch = words[i].charAt(idx);
if (ch == key) {
words[i] = words[i].replaceFirst(str(ch), CHAR);
}
}
}
The problem I have with this code is that you can replace chars in more than one word at a time. For example if you type the key “w” on your keyboard it replaces “w” with “-” from all words which starts which “w” (“which” and “who”). Here I want the program to only replace chars from one word at a time. You therefore would need to type the whole word before you can begin typing the other words.
I really hope someone will help me with this
Vestergaard