import java.util.Scanner; public class TheStringMenu { private String input; private Scanner console; public TheStringMenu() { console = new Scanner(System.in); System.out.println("Give me your String:"); input = console.nextLine(); } public void go(){ int choice = 0; while(choice != 4) { menu(); choice = console.nextInt(); console.nextLine(); switch(choice) { case 1: findLetter(); break; case 2: countWords(); break; case 3: changeWord(); break; case 4: System.out.println("Goodbye"); break; default: System.out.println("That is not a choice, try again."); } } } public void menu() { System.out.println("Press 1 to count the occurrence of a particular letter."); System.out.println("Press 2 to count the total words in your input sentance."); System.out.println("Press 3 to change your input sentance."); System.out.println("Press 4 to exit."); System.out.println("What do you want to do?"); } public void findLetter() { System.out.println("What letter do you want to count?"); String let = console.nextLine(); char letter = let.charAt(0); int find = -1; int count=0; do{ find = input.indexOf(letter, find+1); if(find!=-1) count++; }while(find!=-1); System.out.println("\n****The letter " + letter + " was found " + count + " times.\n\n"); } public void countWords() { int find = -1; int count=0; do{ find = input.indexOf(" ", find+1); if(find!=-1){ count++; while(input.charAt(find+1) == ' ') find++; } }while(find!=-1); count++; System.out.println("\n****There are " + count + " words in the input\n\n"); } public void changeWord() { System.out.println("What is your new input sentance?"); input = console.nextLine(); System.out.println("****Your input sentance has been changed to \"" + input + "\"\n\n"); } }