I am working on a program that will scramble a word and the person has to guess that word.
How do I split a vector of strings into two vectors of strings?
I am given a Vector of Strings from a text file that has a word:hint type of layout. The word is separated by a colon from the hint. I need to create two vectors that separate each word but keep it associated with the hint so that when I click "hint" the correct hint comes up.
I've tried creating a loop but I'm not exactly sure how the loop should be stated. I'm pretty much lost here, any help would be appreciated.
You're on the right track. Loop through your original Vector and for each word:hint item, split them into two separate Strings and add them to two new Vectors.
Since you are adding them into two Vectors, the word and hint will have the same index on the two vectors. That will keep them "associated".
Look at String.split.
You can store the words/hints in two arrays with the same index, or you can put them in a HashMap with map.put(wordA, hintA). A HashMap won't keep the order of the words; if you do need to maintain the order, you can use a LinkedHashMap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With