Unused words in a given String in java OR Remove used words in a string in java

Unused words in a given String in java
SOURCE : UnusedWords.java


import java.util.ArrayList;

import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;

public class UnusedWords {
public static void main(String[] args) {
String s = "To be or not to be - that is the question: Whether it is nobler in the mind to suffer, the slings and arrows of outrageous fortune. Or to take up arms against a sea of troubles, and by opposing end them";
StringTokenizer st = new StringTokenizer(s, " ");
List ls = new ArrayList<>();

HashSet hst = new HashSet();
hst.add("a");
hst.add("be");
hst.add("that");
hst.add("the");
hst.add("this");
hst.add("or");
hst.add("to");

while (st.hasMoreTokens()) {
String Currentword = st.nextToken();
if (!hst.contains(Currentword.toLowerCase())) {

ls.add(Currentword.toLowerCase());

}
}
for (String srt : ls) {

System.out.println(srt.toString());

}
}

}



INPUT

String s = "To be or not to be - that is the question: Whether it is nobler in the mind to suffer, the slings and arrows of outrageous fortune. Or to take up arms against a sea of troubles, and by opposing end them";



OUTPUT

not
-
is
question:
whether
it
is
nobler
in
mind
suffer,
slings
and
arrows
of
outrageous
fortune.
take
up
arms
against
sea
of
troubles,
and
by
opposing
end
them









Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment