Question

In: Computer Science

Write a program named StringWorks.java that asks the user to input a line of text from...

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

  1. the list of words in the sentence including duplicates
  2. A sorted list of the words (alphabetically)
  3. A sorted list of words listed backwards (where z comes before a)
  4. A randomly shuffled list of works
  5. the list of words in the sentence alphabetically removing duplicates.

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

Solutions

Expert Solution

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


Related Solutions

Write a complete Python program that asks the user for a line of text (not just...
Write a complete Python program that asks the user for a line of text (not just a single word), and then tells the user whether the string is a palindrome (same forward and backward) if you ignore case and non-alphabetic characters. The following methods and functions may be useful: str.upper(), str.isalpha() -> bool, reversed(str) -> sequence, str.find(str) -> int, str.replace(str, str), str.center(int). Unless otherwise specified, they all return a string.
Write a program that reads a line of text input by the user and places each...
Write a program that reads a line of text input by the user and places each word in a TreeSet. Print the elements of the TreeSet to the screen. This will cause the elements to be printed in ascending order. Using Eclipse for this. TreeSetUse.java.
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Write a program, ArrayRange, that asks the user to input integers and that displays the difference...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference between the largest and the smallest. You should ask the user the number of integers s/he would like to enter (this will allow you to set the length of the array). Allow the user to input all the numbers and then store them in the array. Search the array for the largest number, then search for the smallest, and finally compute the calculation. Display...
Write a program compare.cpp that asks the user to input two dates (the beginning and the...
Write a program compare.cpp that asks the user to input two dates (the beginning and the end of the interval). The program should check each day in the interval and report which basin had higher elevation on that day by printing “East” or “West”, or print “Equal” if both basins are at the same level. Example: $ ./compare Enter starting date: 09/13/2018 Enter ending date: 09/17/2018 09/13/2018 West 09/14/2018 West 09/15/2018 West 09/16/2018 West 09/17/2018 West Explanation: Date East (ft)...
Write a JAVA program in eclipse (write comment in every line) that asks the user to...
Write a JAVA program in eclipse (write comment in every line) that asks the user to enter two numbers and an operator (+, -, *, / or %), obtain them from the user and prints their sum, product, difference, quotient or remainder depending on the operator. You need to use OOP techniques (objects, classes, methods….). This program must be place in a loop that runs 3 times.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT