Untitled
/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license */ package com.mycompany.mavenproject3; /** * * @author Administrator */ import java.util.Scanner; public class Mavenproject3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (true) { displayMenu(); String choice = scan.nextLine(); switch (choice) { case "1": System.out.print("-Enter a word: "); String name1 = scan.nextLine(); Activity1(name1); break; case "2": System.out.print("-Enter the first String: "); String str1 = scan.nextLine(); System.out.print("-Enter the second String: "); String str2 = scan.nextLine(); Activity2(str1,str2); break; case "3": System.out.print("-Enter a word: "); String name2 = scan.nextLine(); Activity3(name2); break; case"4": System.out.print("-Enter a word to reverse: "); String name3 = scan.nextLine(); Activity4(name3); break; case "5": System.out.print("-Enter a word: "); String nameToSearch = scan.nextLine(); char ch = Act5(scan); Activity5(nameToSearch, ch); break; case "6": System.out.println("-Thank you for using the program. Goodbye :)"); return; default: System.out.println("-Invalid choice, Please Try Again!"); } } } public static void displayMenu() { System.out.println("\t=String Manipulation Laboratory="); System.out.println("1.String Length and Charactcer Extraction"); System.out.println("2.Convert and Combine String"); System.out.println("3.Extract Substring"); System.out.println("4.Reverse String"); System.out.println("5.Find and Count Characters"); System.out.println("6.Exit"); System.out.print("-Enter a choice: "); } public static void Activity1(String name) { System.out.println("-Length of the word: " + name.length()); System.out.println("-First Character: " + name.charAt(0)); System.out.println("-Last Character: " + name.charAt(name.length() - 1)); } public static void Activity2(String str1, String str2) { System.out.println("-Converted to Uppercase: " + str1.toUpperCase() + "" + str2.toLowerCase()); } public static void Activity3(String input){ if (input.length() < 5) { System.out.println("-Substring: " + input); } else { System.out.println("-Substring: " + input.substring(0, 5)); } } public static void Activity4(String name){ String result = ""; for (int i = name.length() - 1; i >= 0; i--) result += name.charAt(i); System.out.println("-Reversed Word: " + result); } public static void Activity5(String name, char ch) { int count = 0; for (int i = 0; i < name.length(); i++) { if (name.charAt(i) == ch) { count ++; } } System.out.println("-The character '" + ch + "' appears '" + count + "" + "' times in the word '" + name + "'."); } public static char Act5(Scanner scan) { char ch = '\0'; while (true) { System.out.print("-Enter a Character to Count: "); String input = scan.nextLine(); if (input.length() == 1) { ch = input.charAt(0); break; } else { System.out.println("-Please Enter a Single Character: "); } } return ch; } }
Leave a Comment