We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there.
I'm working on an App.
Part of what it does, is print Strings of text to the screen, one by one, as the user clicks through. Most users won't be fluent in the language of the App, and I'm working on a kind of help feature.
I want users to be able to type a word that they don't understand, and print the translation to the screen.
I've made a new sketch (based on this and this) to develop the mechanism:
float txtPos= random(100, 200);
String myText = "";
void setup() {
size(800, 600);
background(#3355cc);
}
void draw() {
fill(#3355cc, 40);
rect(0, 0, width, height);
text(myText, 0, 0, width, height);
}
void keyPressed() {
textSize(160);
fill(#E8B600);
text(key, txtPos, 240);
txtPos += 70;
if (txtPos>width) {
txtPos-= width;
}
if (keyCode == BACKSPACE) {
if (myText.length()>0) {
myText = myText.substring(0, myText.length()-1);
}
} else if (keyCode == DELETE) {
myText = "";
} else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT)
myText = myText + key;
if (keyCode == ENTER) {
mousePressed();
}
print(key );
}
void mousePressed() {
if (myText.equalsIgnoreCase("madra")) {
println();
print("Madra means dog!");
myText = "";
println();
} else {
println();
print("Ní thuigim "+myText);
myText = "";
println();
}
}
That works, except when ENTER is used to activate mousePressed(). I think I can fix that. But it's not a great system. I think it would be much better if the program could accept a String from the user, and check to see if any part of that string is a substring of the App's text. (as opposed to making a giant if else or switch statement block) Then I could print a more helpful answer.
Any thoughts on how to do that?
currently trying variations of
if (myText.substring(0,myText.length()-1).equalsIgnoreCase("madra"))
//but that ain't it...
Answers
You could use the
contains()
function. As always, the API is your best friend.Try this
Great! Thanks! will try those suggestions.
Thanks for your suggestions. I'm working on it right now.
So, I think the thing to do is make an array of Strings of searchable keywords from the main text. Something like:
and then on mousePressed() loop through ss[] looking to see if ss[i].toLowercase().contains(userInput.toLowerCase).
Then if for example ss[4] is typed by the user, the program can print ssExplain[4] to the screen.
Something like that?
Not sure if this is what you want but here's a probable solution:
NOTE: This textbox function treats the Enter key as an ordinary key and passes it to the string. If you want to use Enter as an exit (from the textbox) key let me know...
thank you so much! I'll consider your code carefully.