Question

Write a Java Program that performs the following functionalities: and you can use if statements, cases,...

Write a Java Program that performs the following functionalities: and you can use if statements, cases, and string methods

Enter a new main sentence

Find a String

Find all incidents of a String

Find and Replace a String

Replace all the incidents of a String

Count the number of words

Count a letter’s occurrences

Count the total number of letters

Delete all the occurrences of a word

Exit

The program will display a menu with each option above, and then ask the user to enter their choice. There should also be a tenth option (option J) to quit, in which case, the program will simply display a goodbye message.

Based on the user’s choice, the program will perform the chosen functionality - more details are given below for each one. Make sure to include proper output for all cases (such as instructions, questions to the user, or directions telling the user to input something specific). Make sure the menu uses letters not numbers!

Note: The program should keep running and displaying the menu after each functionality, until the user chooses to exit! Make sure to get a default sentence from the user before going into the menu options.

Descriptions of Each Option:

Enter a new main sentence: Ask the user to input a new sentence. This sentence will be later on used in the other menu operations ex: (Find a String, Find all incidents of a String, etc.)

Find a String: Ask the user to input a word, then display the index of the first occurrence of the word in the main sentence. If the word is not found make sure to output the word is not found!

TIP: If you get a runtime error because the word is not found, then your if statement structure is incorrect!

Find all incidents of a String: Ask the user to input a word, then display the index of every occurrence of that word in the main sentence. If the word is not found make sure to output the word is not found!

Find and Replace a String: Prompt the user to input a word, then ask the user to input another new word for replacement. Lastly, locate the word in the main sentence and replace it with the new word. If the user inputs a string that’s not found in the main sentence make sure to output the word is not found.
Note: This option only replaces the first occurence of the String.

Replace all incidents of a String: Prompt the user to input a word, then ask the user to input another new word for replacement. Lastly, locate the all the occurrences of the word in the main sentence and replace all of them with the new word. If the user inputs a string that’s not found in the main sentence make sure to output the word is not found.

Count the number of words: Display the number of words in the main sentence.
TIP: Think of what usually separates words and just count the number of occurrences for that.

Count a letter’s occurrences: Prompt the user to enter a letter. Store that letter in a char variable and then display how many times the char occurs in the main sentence.

Count the total number of letters: Store the main sentence in a temporary variable, try to trim it from spaces. Then use the length method to count how many letters/characters there are in the main sentence.

Delete all the occurrences of a word: Prompt the user to enter a word, then delete all the occurrences of that word in the main sentence then display the updated main sentence.

Note: If the user types “at”, then all occurrences of “at” should be removed. Example: “station” becomes “stion”. On the other hand, if the user types “ at ” with spaces then “station” is not altered.

Exit: Simply display a goodbye message. End the program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

****This requires a lot of effort so please drop a like if you are satisfied with the solution****

I have satisfied all the requirements of the question and I'm providing the screenshots of code and output for your reference..

Code:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
       Scanner in=new Scanner(System.in);
       char choice;
       String mainString="";
       while(true){
       System.out.println("\na. Enter a new main sentence");
       System.out.println("b. Find a string");
       System.out.println("c. Find all incidents of a string");
       System.out.println("d. Find and Replace a string");
       System.out.println("e. Replace all the incidents of a string");
       System.out.println("f. Count the number of words");
       System.out.println("g. Count a letter's occurences");
       System.out.println("h. Count the total number of letters");
       System.out.println("i. Delete all occurences of a word");
       System.out.println("j. Exit");
       System.out.print("\nchoice => ");
       choice=in.next().charAt(0);
       System.out.println();
       in.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
       if(choice=='a'){
       System.out.print("Enter the main sentence: ");
       mainString=in.nextLine();
       }
       if(choice=='b'){
       System.out.print("Enter a word to search: ");
       String word1=in.nextLine();
       if(mainString.indexOf(word1)!=-1){
       System.out.println("The word "+word1+" is found in the main sentence at "+mainString.indexOf(word1));
       }
       else{
       System.out.println("Word not found!");
       }
       }
       if(choice=='c'){
       int index=0;
       System.out.print("Enter a word to search: ");
       String word2=in.nextLine();
       if(mainString.indexOf(word2)!=-1){
       System.out.print("The word is found at indexes: ");
while(index != -1){
index = mainString.indexOf(word2, index);
if (index != -1) {
System.out.print(index+" ");
index++;
}
}
       }
       else{
       System.out.println("Word not found!");
       }
       }
       if(choice=='d'){
       System.out.println("Enter word to replace: ");
       String word3=in.nextLine();
       if(mainString.indexOf(word3)!=-1){
       System.out.println("Enter new word: ");
       String newWord=in.nextLine();
       mainString=mainString.replaceFirst(word3,newWord);
       System.out.println("The string after replacing: "+mainString);
       }
       else{
       System.out.println("Word not found!");
       }
       }
       if(choice=='e'){
       System.out.println("Enter word to replace: ");
       String word4=in.nextLine();
       if(mainString.indexOf(word4)!=-1){
       System.out.println("Enter new word: ");
       String word5=in.nextLine();
       mainString=mainString.replaceAll(word4,word5);
       System.out.println("The string after replacing: "+mainString);
       }
       else{
       System.out.println("Word not found!");
       }
       }
       if(choice=='f'){
       int count = 1;
       for (int i = 0; i < mainString.length() - 1; i++)
       {
           if ((mainString.charAt(i) == ' ') && (mainString.charAt(i + 1) != ' '))
           {
               count++;
           }
       }
        System.out.println("Number of words in main string = " + count);
       }
       if(choice=='g'){
       System.out.print("Enter a letter to count: ");
       char c=in.next().charAt(0);
       int j=0;
       for(int i=0;i<mainString.length();i++){
       if(mainString.charAt(i)==c)
       j++;
       }
       System.out.println("Number of occurences of letter "+c+" is: "+j);
       }
       if(choice=='h'){
       int counter = 0;
for (int i = 0; i < mainString.length(); i++) {
if (Character.isLetter(mainString.charAt(i)))
counter++;
}
System.out.println("Main string has "+counter + " letters.");
       }
       if(choice=='i'){
       System.out.print("Enter a word to delete from main string: ");
       String word5=in.nextLine();
       mainString = mainString.replaceAll("(?i)\\b" + word5 + "\\b *", "").trim();
       System.out.println("The string after deleting "+word5+" is: "+mainString);
       }
       if(choice=='j'){
       System.out.println("Good Bye!");
       break;
       }
       }
   }
}

output Screenshots:

Code Screenshots:

Add a comment
Know the answer?
Add Answer to:
Write a Java Program that performs the following functionalities: and you can use if statements, cases,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Code Create a tool in which a user can enter a string (up to 25...

    C Code Create a tool in which a user can enter a string (up to 25 characters) and choose how they wish to manipulate the string from a menu your program will display (see the run-through). The program will continue to ask for commands until the user enters the “quit” command. The commands are: • “Replace All” If a user types “replace all” using ANY sort of capitalization, your program will prompt the user to enter the character to change...

  • Write MIPS code, please. Following: Write a program to be used by the Wisconsin DNR to...

    Write MIPS code, please. Following: Write a program to be used by the Wisconsin DNR to track the number of Deer Hunting Licenses issued in a county and the state. It will display a menu with 5 choices numbered 1-5. They are (1) update the count, (2) display the number of licenses already issued to a county whose number is input, (3) display the county number and the number of licenses already issued to a range of counties whose starting...

  • This program will store a roster of most popular videos with kittens. The roster can include...

    This program will store a roster of most popular videos with kittens. The roster can include at most 10 kittens.You will implement structures to handle kitten information. You will also use functions to manipulate the structure. (1) Create a structure kitten. The structure should contain the following attributes: name; string color; string score; integer Important! The name of the structure and each of its field must match exactly for the program to work and be graded correctly. (2) Create a...

  • I need help with my Java code. A user enters a sentence and the program will...

    I need help with my Java code. A user enters a sentence and the program will tell you what words were duplicated and how many times. If the same word is in the sentence twice, but one is capitalized, it will not count it as a duplicate. How can I make the program read the same word as the same word, even if one is capitalized and the other is not? For example, "the" and "The" should be counted as...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Problem #1 Create a program that performs the following functions: Uses character arrays to read a...

    Problem #1 Create a program that performs the following functions: Uses character arrays to read a user's name from standard input Tells the user how many characters are in her name Displays the user's name in uppercase Create a program that uses the strstr() function to search the string "When the going gets tough, the tough stay put! for the following occurrences (display each occurrence found to standard output): "Going" "tou" "ay put!" Build a program that uses an array...

  • Java 7.17 Clone of LAB*: Program: Soccer team roster This program will store roster and rating...

    Java 7.17 Clone of LAB*: Program: Soccer team roster This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0-99, but this is NOT enforced by the program nor tested) and the player's rating (1-9, not enforced like jersey numbers). Store the jersey numbers in one int array and the ratings in another int...

  • Write a menu driven program to demonstrate the use of array and switch. It must be...

    Write a menu driven program to demonstrate the use of array and switch. It must be written in C language . Here is the menu - Press 1 to add a number to the array. Press 2 to display the numbers entered in the array so far Press 3 to find the average of the numbers entered. Press 4 to exit. Option 1 - The user should be able to enter 20 numbers only. If all twenty numbers are entered...

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT