Reputation: 279
I have following type of sentences. I want to divide sentence into two by two parts like following example.
Example:
Nimal read/write book/newspaper from his pen.
I want to divide following way above sentence and add into arraylist.
Nimal/read
Nimal/write
read/book
read/newspaper
write/book
write/newspaper
book/from
newspaper/from
from/his
his/pen.
That's mean I want to get two words phrases from the next word.I have divide and added sentences.But I haven't clear idea to what next to do.
ArrayList<String> wordArrayList2 = new ArrayList<String>();
String sentence="Nimal read/write book/newspaper from his pen";
for(String wordTw0 : sentence.split(" ")) {
wordArrayList2.add(wordTw0);
}
Upvotes: 0
Views: 1844
Reputation: 46841
A simple program using String#split()
method.
steps to follow:
Sample code:
String str = "Nimal read/write book/newspaper from his pen.";
ArrayList<String> wordArrayList = new ArrayList<String>();
String[] array = str.split("\\s+"); // split based on one or more space
for (int i = 0; i < array.length - 1; i++) {
String s1 = array[i];
String s2 = array[i + 1];
String[] a1 = s1.split("/"); //split based on forward slash
String[] b1 = s2.split("/"); //split based on forward slash
for (String a : a1) {
for (String b : b1) {
String word = a + "/" + b;
wordArrayList.add(word);
System.out.println(word);
}
}
}
output:
Nimal/read
Nimal/write
read/book
read/newspaper
write/book
write/newspaper
book/from
newspaper/from
from/his
his/pen.
Upvotes: 2