Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Jacob
- *
- */
- import java.util.*;
- public class Assignment2 {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String choice = null;
- String mainSentence = null;
- String word = null;
- String word2 = null;
- String tempSentence;
- char letter;
- int index;
- int counter = 0;
- boolean quit = false;
- System.out.println("Please enter a main sentence: ");
- mainSentence = input.nextLine();
- while(quit == false) {
- System.out.println("Please choose from one of the options below:\n A. Enter a new main sentence\n B. Find a string\n C. Find all incidents of a string\n D. Find and Replace the String\n"
- + " E. Replace all incidents of a string to another one\n F. Count the number of words\n G. Count the number of occurences of a letter\n H. Count the total number of letters\n"
- + " I. Delete all the occurences of a word\n J. Exit\n\n Enter your choice: " );
- choice = input.nextLine();
- switch (choice) {
- case "A":
- System.out.println("Enter a new main sentence: ");
- mainSentence = input.nextLine();
- System.out.println("The new main sentence is: " + mainSentence + "\n");
- break;
- case "B":
- System.out.println("Enter a word you would like to find: ");
- word = input.nextLine();
- System.out.println("The first occurance of " + word + " is at " + mainSentence.indexOf(word));
- break;
- case "C":
- System.out.println("Enter a word you would like to search for: ");
- word = input.nextLine();
- counter = mainSentence.indexOf(word);
- while (counter >= 0) {
- System.out.println(counter);
- counter = word.indexOf(word, counter + 1);
- }
- System.out.println(word + " occurs " + counter + " times.\n");
- break;
- case "D":
- System.out.println("Enter the string you would like to search for: ");
- word = input.nextLine();
- System.out.println("Enter the string you would like to replace it with: ");
- word2 = input.nextLine();
- if(mainSentence.contains(word) == true) {
- mainSentence = mainSentence.replaceFirst(word, word2);
- System.out.println("The new main sentence is: " + mainSentence);
- }
- else {
- System.out.println("Search string not found!\n");
- }
- break;
- case "E":
- System.out.println("Enter the word you would like to replace: ");
- word = input.nextLine();
- System.out.println("Enter what you would like to replace it with: ");
- word2 = input.nextLine();
- mainSentence = mainSentence.replaceAll(word, word2);
- System.out.println("The new sentence is " + mainSentence);
- break;
- case "F":
- counter = mainSentence.length() - mainSentence.replace(" ", "").length() + 1;
- System.out.println("The number of words is: " + counter);
- break;
- case "G":
- System.out.println("Enter a letter to count the number of occurrences: ");
- letter = input.next().charAt(0);
- for(int i = 0; i < mainSentence.length(); i++) {
- if(mainSentence.charAt(i) == letter)
- counter++;
- }
- System.out.println(letter + " occurs " + counter + " times");
- break;
- case "H":
- tempSentence = mainSentence.replaceAll(" ", "");
- System.out.println("Your sentence has " + tempSentence.length() + " letters.");
- break;
- case "I":
- System.out.println("Please enter a word you would like to delete: ");
- word = input.nextLine();
- mainSentence = mainSentence.replaceAll(word, "");
- System.out.println("The new main sentence is:\n " + mainSentence);
- break;
- case "J":
- quit = true;
- System.out.println("Goodbye!");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement