We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Error on line 43: I need "cur" to be entered into my "to_match" file whenever it's not already there. Never used saveStrings before and it's doing my head in. Please advise. Thank you!!
String[] to_match;
String[] resps;
String top = "Ask me anything.";
String cur = "";
final int QUERY_LIMIT = 35;
void setup() {
size(1200, 500);
textSize(20);
fill(0);
PFont jackFont;
jackFont = loadFont("CourierNewPSMT-48.vlw");
textFont(jackFont);
to_match = loadStrings("to_match_file.txt");
resps = loadStrings("resps_file.txt");
}
void draw() {
background(255);
fill(0);
text(top, 20,20, 1200,500);
fill(225, 0, 0);
text("> "+ cur+((millis()%1000)<500?(" "):("_")), 20,440, 1200,400);
}
void keyPressed() {
if (key>='a'&&key<='z') {
if (cur.length()<=QUERY_LIMIT) cur = cur + char(key+'A'-'a');
}
if (key>='A'&&key<='Z') {
if (cur.length()<=QUERY_LIMIT) cur = cur + char(key);
}
if (key==' '&&(cur.length()<=QUERY_LIMIT)) cur=cur+' ';
if (key=='?'&&(cur.length()<=QUERY_LIMIT)) cur=cur+'?';
if (key==DELETE||key==BACKSPACE) {
if (cur.length()>0) cur=cur.substring(0, cur.length()-1);
}
if (key==ENTER||key==RETURN) {
if(cur.charAt(cur.length()-1)!='?'){
top = "Please ask a QUESTION.";
} else {
top = "I don't know. Ask something else.";
saveStrings("to_match_file.txt", cur);
}
for(int i=0; i < to_match.length; i++){
if(cur.equals(to_match[i])){
top=resps[i];
}
}
cur="";
}
}
Answers
Based on the reference, (https://processing.org/reference/saveStrings_.html) saveStrings is expecting and array of strings. A possible way to do it:
Kf
That does get rid of the error, but now it overwrites that entire text document whenever new data is entered... Is there a way to add it as a new line so that new data can continue to accumulate with input?
I can either do this, which doesn't do anything:
Or I can do this, which replaces the entire document with the new data:
Read the data w/ loadStrings(), append your new String to it w/ append(), then saveStrings() it.
https://Processing.org/reference/append_.html
He's already read the data in line 14. He could just append it to that and save the whole lot.
But that way the two arrays will end up different lengths and that'll break line 47.
GoToLoop I managed to get it basically working with a wonky amalgam of things... Still appends the document for all questions instead of just unanswered ones, but that can be a headache for another night. This is what my program looks like now:
I couldn't quite get it to behave like you said it could with loadStrings -> append -> saveStrings... But if it can truly be done better and more elegantly with those then I'm all ears