In: Computer Science
Write a program named StringWorks.java that asks the user to input a line of text from the keyboard. Ask the user if they want their answers case sensitive or not. You output should be
You need to remove punctuation. Use the Character.isLetterOrDigit() method.
Sample output:
Please type in a sentence:
I love java, and Java loves me! That is cool, yes?
Do you want case sensitivity? (y/n):
n
List of words: i love java and java loves me that is cool yes
Sorted alphabetically: and cool i is java java love loves me that yes
Sorted backwards: yes that me loves love java java is i cool and
Shuffled: java that cool is i love and loves me java yes
Without duplicates, sorted alphabetically: and cool i is java love loves me that yes
If you have any problem with the code feel free to comment.
Program
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);// for taking console input
// taking sentence
System.out.print("Please type in a
sentence: ");
String str = sc.nextLine();
// taking sensitivity
choice
System.out.print("Do you want case
sensitivity? (y/n): ");
String ch = sc.nextLine();
// spliting the sentence into
array of words
String[] words = str.split("
");
ArrayList<String> list = new
ArrayList<>();
for (int i = 0; i <
words.length; i++) {
if
(ch.equalsIgnoreCase("y")) {
// replacing all punctuations using regex
// \\W matches all non words and replaces them
with blank
// list.add(words[i].replaceAll("\\W", ""));
another way to replace punctuation
list.add(removePunctuation(words[i]));
} else if
(ch.equalsIgnoreCase("n")) {// when casesensitivy is not
allowed
list.add(removePunctuation(words[i]).toLowerCase());
}
}
// a. show list of words
System.out.print("List of words:
");
for (String i : list) {
System.out.print(i + " ");
}
System.out.println();
// b. show the list of sorted
words
Collections.sort(list);
System.out.print("Sorted
alphabetically: ");
for (String i : list) {
System.out.print(i + " ");
}
System.out.println();
// c. show the list . in
reverse
Collections.reverse(list);
System.out.print("Sorted backwards:
");
for (String i : list) {
System.out.print(i + " ");
}
System.out.println();
// d. shuffle the list
Collections.shuffle(list);
System.out.print("Shuffled:
");
for (String i : list) {
System.out.print(i + " ");
}
System.out.println();
// e. remove duplicates from the
list
list =
removeDuplicates(list);
Collections.sort(list);
System.out.print("Without
duplicates, sorted alphabetically: ");
for (String i : list) {
System.out.print(i + " ");
}
System.out.println();
sc.close();
}
private static <T> ArrayList<T> removeDuplicates(ArrayList<T> list) {
// creating a set
Set<T> set = new
LinkedHashSet<>();
// adding list elements to the
set
// set doesn't allow duplicate
elements
set.addAll(list);
// removing all the elements from
the list
list.clear();
// adding all the elemnets of set
to the list
list.addAll(set);
return list;
}
//for removing punctuation
private static String removePunctuation(String word)
{
if
(!Character.isLetterOrDigit(word.charAt(0))) {//checking the
begning of the word
word =
word.replace(word.charAt(0), ' ');
}
//checking the end of the
word
if
(!Character.isLetterOrDigit(word.charAt(word.length() - 1)))
{
word =
word.replace(word.charAt(word.length() - 1), ' ');
}
return word.trim();
}
}
Output