-a- well you can start from a empty file,
-b- still new words should be checked for have no double
( i tell you that arrays are not funny, like deleting a double later is tricky )
-c- but reading a not existing file gives error ( so you could create one )
actually that is a big problem, easy we can exchange code here at the forum
but as soon files ( and subdir ) are involved the sharing is not so easy
( i can give you a zip, but would you want download a zip from a unknown source? )
I think this code is a good challenge for me to learn, I will follow your indication like starting from an empty file.
I think is ok to have double so that the code can know witch one is the most āusedā word otherwise they will be all the same size, no?
Yes reading a empty file give error but i was thinking to solve that problem laterā¦
Yes I would download the zip from you. But I donāt want you to do the code for me I would like to learn how to develop it by my own.
actually i think there might come much more functionality to this,
so i doubt that on long terms you will be happy with that text file.
a database? how is your SQL?
processing works great with sqlite3, and same easy about file handling
like that text file you use now.
( can not compare with the install and use of MySQL 8 )
Well the code Iām trying to develop is for an installation. Iāll use only once for one evening. Iām not an expert and so I trust what you say but creating a DB it seams to me not necessary for the use I will do of it. No?
Not sure if this fits the case, but maybe thereās no need to call expand nor append. Maybe concat is enough? concat returns a new array thatās the combination of two arrays.
String filename ="data/MyText.txt";
String[] oldlines = loadStrings(filename); // load old words into a String array
String answer = "bicycle flower"; // simulated answer
String[] newlines = split(answer, ' '); // split answer into words (a String array)
saveStrings(filename, concat(oldlines, newlines)); // save both arrays to the file
But if you know that the answer only contains one word, then thereās no reason to split it by spaces, and itās even shorter:
String filename ="data/MyText.txt";
String[] lines = loadStrings(filename);
String answer = "bicycle";
saveStrings(filename, append(lines, answer)); // save the old array + the new word
Which just adds one word to the file.
Now you should be able to create a function that adds a word to a file. It would look like this:
void addWordToFile(String word, String filename) {
// I leave this as an exercise :)
}
Hey Hamoid
thanks for the elegant hint! It work out!
this is the code
String wordFile = "data/H-Farm.txt";
String[] msg;
void webSocketServerEvent(String msg) { //listen the input from microphone and add it in the string call msg
println(msg); // print the line with voice input
addWordToFile(msg, "data/H-Farm.txt"); // call addWordToFile
}
void addWordToFile(String msg, String wordFile) { // create two strings: msg and wordFile
String[] oldlines = loadStrings(wordFile); // create a string (empty) call old.. and i fill it with what there's already in the text file
String answer = msg ; // not clear what this line does..
String[] newlines = split(msg, ' '); // create string and fill it with string "msg" splitted every empty space
saveStrings(wordFile, concat(oldlines, newlines)); //save all
}
I donāt understand why I am creating so many strings (msg, wordFile, oldlines, answer, newlines) those are all string right?
I comment each line with what i guess the code does, would you mind please to check my comment and see if they are right?
thank you a lot
as well for the exercise
You could delete the first two lines, because you are not using them anywhere. They are global variables, and they get overwritten by local variables with the same names.
After void addWordToFile I would not write ācreate two stringsā. They are actually arguments / parameters for the addWordToFile function. The values of those two variables are received from outside, from wherever you are calling the function.
You can also delete String answer = msg; since you are not using answer anywhere.
@GoToLoop in the guidelines it recommends not to post links or code without explanations