import java.util.Scanner; public class StringParser { private Scanner input; private String w1, w2, w3, w4; public StringParser() { input = new Scanner(System.in); System.out.println("Give the first word:"); w1 = input.nextLine(); System.out.println("Give the second word:"); w2 = input.nextLine(); System.out.println("Give the third word:"); w3 = input.nextLine(); System.out.println("Give the fourth word:"); w4 = input.nextLine(); } public void startProgram() { int choice; do{ menu(); choice = input.nextInt(); input.nextLine(); if(choice == 1) largest(); else if(choice == 2) search(); else if(choice == 3) subSearch(); else if(choice == 4) System.out.println("Goodbye"); else System.out.println("That's not a choice"); }while(choice != 4); } private void menu() { System.out.println("\nPress 1 to find the largest String."); System.out.println("Press 2 to search for a a particular String."); System.out.println("Press 3 to search for a particular substring anywhere in the string." ); System.out.println("Press 4 to exit."); } private void largest() { String largest = w1; if(largest.length() < w2.length()) largest = w2; if(largest.length() < w3.length()) largest = w3; if(largest.length() < w4.length()) largest = w4; System.out.println(largest + " is the largest at a size of " + largest.length()); } private void search() { System.out.println("Give a new word to be searched for:"); String word = input.nextLine(); if(word.equals(w1)) System.out.println("Word 1 is the same"); else if(word.equals(w2)) System.out.println("Word 2 is the same"); else if(word.equals(w3)) System.out.println("Word 3 is the same"); else if(word.equals(w4)) System.out.println("Word 4 is the same"); else System.out.println(word + " was not found."); } private void subSearch() { System.out.println("Give a substring to be searched for:"); String sub = input.nextLine(); boolean flag = false; if(w1.contains(sub)) { flag = true; System.out.println("Word 1 has the substring"); } if(w2.contains(sub)) { flag = true; System.out.println("Word 2 has the substring"); } if(w3.contains(sub)) { flag = true; System.out.println("Word 3 has the substring"); } if(w4.contains(sub)) { flag = true; System.out.println("Word 4 has the substring"); } if(!flag) System.out.println("Substring " + sub + " was not found."); } }