Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting vectors

Tags:

java

oop

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.

like image 859
user1056611 Avatar asked Dec 03 '25 07:12

user1056611


2 Answers

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".

like image 178
Kal Avatar answered Dec 05 '25 19:12

Kal


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.

like image 22
toto2 Avatar answered Dec 05 '25 20:12

toto2