Is there a way to take the following sentence:
"I want this split up into pairs"
and generate the following list using java:
"I want", "want this", "this split", "split up", "up into", "into pairs"
Is there a way to take the following sentence:
"I want this split up into pairs"
and generate the following list using java:
"I want", "want this", "this split", "split up", "up into", "into pairs"
There is a way, lots of ways one of these can be:
String string = "I want this split up into pairs";
String[] words = string.split(" ");
List<String> pairs = new ArrayList<String>();
for (int i = 0; i < words.length-1; ++i) {
pairs.add(words[i] + " " + words[i+1]);
}
System.out.println(pairs);
This is a basic algo
Tokenize or split your sentence
I want this split up into pairs -> I, want, this, split, up, into, pairs
Then
first = read first word
while(there are more words to read){
second = read next word
Print first + " " + second
first = second
}
An example is as follows:
String text = "I want this split up into pairs";
String[] words = text.split(" ");
//Print 2 combined words
for(int i=1;i<words.length;i++)
{
System.out.printf("%s %s\n", words[i-1],words[i]);
}
Output in Console:
I want
want this
this split
split up
up into
into pairs
Not a huge java person, so not sure if this code is 100% but it's a general idea for one option.
String x = "I want this split up into pairs";
String[] parts = x.split(" ");
List<String> newParts = new ArrayList<String>();;
for (int i = 0; i < parts.length()-1; i++) {
newParts.add(parts[i]+parts[i+1]);
}
Then, the newParts List will have all your pairs