String Functions Made Easy

 String Functions Made Easy

/**

* Project Name : String Functions Made Easy

* Author : Mr. Sumit Kole.

*/

import java.util.*;

class StringFuntionMadeEasy

{

String result,userInput1, userInput2;

char resultChar;

Scanner sc;

int choice;

public StringFuntionMadeEasy()

{

// initialise instance variables

result="";

userInput1=" ";

userInput2=" ";

resultChar='\u0000';

choice=0;

sc=new Scanner(System.in);

}

public void menu(){

System.out.println("\n\t\t\t ------String Functions Made Easy----

-");

System.out.println("\n\t\t\t Press 1 to Demonstrate CharAt()");

System.out.println("\n\t\t\t Press 2 to Demonstrate

compareTo()");

System.out.println("\n\t\t\t Press 3 to Demonstrate startsWith()

");

System.out.println("\n\t\t\t Press 4 to Demonstrate endsWith()

");

System.out.println("\n\t\t\t Press 5 to Demonstrate equalto() ");

System.out.println("\n\t\t\t Press 6 to Demonstrate

equalsIgnoreCase()");

System.out.println("\n\t\t\t Press 7 to Demonstrate indexOf() ");

System.out.println("\n\t\t\t Press 8 to Demonstrate lastIndexOf()

");

System.out.println("\n\t\t\t Press 9 to Demonstrate replace()");

System.out.println("\n\t\t\t Press 10 to Demonstrate

toUpperCase()");

System.out.println("\n\t\t\t Press 11 for of Exit");

System.out.println("\n\t\t\t

_____________________________________");

System.out.print("\n\t\t\t Please Enter Your Choice: ");

try{

sc=new Scanner(System.in);

choice = sc.nextInt();

sc.close();

if(choice<1 || choice>11){

System.out.println("\t\t\t Please Enter Numbers between 1

to 11 Only..");

menu();

}

operateMenu(choice);

}

catch(InputMismatchException e){

System.out.println("\t\t\t Please Enter Numbers Only..");

sc.close();

menu();

}

}

public String get(){

sc = new Scanner(System.in);

String input=sc.nextLine();

sc.close();

return input;

}

public void operateMenu(int choice){

switch(choice) {

case 1:

demo_charAt();

break;

case 2:

demo_compareTo();

break;

case 3:

demo_startsWith();

break;

case 4:

demo_endsWith();

break;

case 5:

demo_equals();

break;

case 6:

demo_equalsIgnoreCase();

break;

case 7:

demo_indexOf();

break;

case 8:

demo_lastIndexOf();

break;

case 9:

demo_replace();

break;

case 10:

demo_toUpperCase();

break;

case 11: System.exit(0);

default: System.out.println("Please enter number between 1 to

10");

}

System.out.println("\n \t\t\t Press 1 to Continue 0 to Exit ");

int number = Integer.parseInt(get());

if(number == 1){

menu();

}

else{

System.exit(0);

}

}

private void demo_charAt(){

System.out.println("\n\t\t\t Returns the character at the

specified index.");

System.out.print("\n\t\t\t Please Enter String ");

userInput1 = get();

System.out.print("\n\t\t\t Please Enter Index Number of character

which you want ");

int index = Integer.parseInt(get());

resultChar=userInput1.charAt(index);

System.out.println("\n\t\t\t Character At "+index+" =

"+resultChar);

}

private void demo_compareTo(){

System.out.println("\n\t\t\t Compares two strings

lexicographically.");

System.out.print("\n\t\t\t Please Enter First String ");

userInput1 = get();

System.out.print("\n\t\t\t Please Enter Second String ");

userInput2 = get();

if(userInput1.compareTo(userInput2)==0){

System.out.println("\n\t\t\t Entered Strings are Equal");

}

else{

System.out.println("\n\t\t\t Entered Strings are Not Equal");

}

}

private void demo_startsWith(){

System.out.println("\n\t\t\t Tests if this string starts with the

specified prefix.");

System.out.print("\n\t\t\t Enter String :");

userInput1=get();

System.out.print("\n\t\t\t Enter Prefix :");

userInput2=get();

if(userInput1.startsWith(userInput2)){

System.out.print("\n\t\t\t Entered String Starts with "+

userInput2);

}

else{

System.out.print("\n\t\t\t Entered String Does Not Starts

with "+ userInput2);

}

}

private void demo_endsWith(){

System.out.println("\n\t\t\t Tests if this string ends with the

specified suffix.");

System.out.print("\n\t\t\t Enter String :");

userInput1=get();

System.out.print("\n\t\t\t Enter Suffix :");

userInput2=get();

if(userInput1.endsWith(userInput2)){

System.out.print("\n\t\t\t Entered String Ends With "+

userInput2);

}

else{

System.out.print("\n\t\t\t Entered String Does Not Ends With

"+ userInput2);

}

}

private void demo_equals(){

System.out.println("\n\t\t\t Compares two strings are equal or

not.");

System.out.print("\n\t\t\t Enter First String For Comparison

:");

userInput1=get();

System.out.print("\n\t\t\t Enter Second String For Comparison

:");

userInput2=get();

if(userInput1.equals(userInput2)){

System.out.print("\n\t\t\t Entered Strings are Equal");

}

else{

System.out.print("\n\t\t\t Entered String are not Equal");

}

}

private void demo_equalsIgnoreCase(){

System.out.println("\n\t\t\t Compares this String to another

String, ignoring case considerations.");

System.out.println("\n\t\t\t Compares two strings are equal or

not.");

System.out.print("\n\t\t\t Enter First String For Comparison

:");

userInput1=get();

System.out.print("\n\t\t\t Enter Second String For Comparison

:");

userInput2=get();

if(userInput1.equalsIgnoreCase(userInput2)){

System.out.print("\n\t\t\t Entered Strings are Equal");

}

else{

System.out.print("\n\t\t\t Entered String are not Equal");

}

}

private void demo_indexOf(){

System.out.println("\n\t\t\t Returns the index of the first

occurrence of the specified character.");

System.out.print("\n\t\t\t Enter String :");

userInput1=get();

System.out.print("\n\t\t\t Enter Character :");

char charInput= get().charAt(0);

if(charInput !='\u0000'){

System.out.print("\n\t\t\t First Occurence Of The Specified

Character ="+userInput1.indexOf(charInput));

}

else{

System.out.print("\n\t\t\t Enter valid character.");

}

}

private void demo_lastIndexOf(){

System.out.println("\n\t\t\t Returns the index within this string

of the last occurrence of the specified character.");

System.out.print("\n\t\t\t Enter String :");

userInput1=get();

System.out.print("\n\t\t\t Enter Character :");

char charInput= get().charAt(0);

if(charInput !='\u0000'){

System.out.print("\n\t\t\t Last Occurence Of The Specified

Character ="+userInput1.lastIndexOf(charInput));

}

else{

System.out.print("\n\t\t\t Enter valid character.");

}

}

private void demo_replace(){

System.out.println("\n\t\t\t Returns a new string resulting from

replacing all occurrences of old Char in this string with new Char.");

System.out.print("\n\t\t\t Enter String :");

userInput1=get();

System.out.print("\n\t\t\t Enter Old Character :");

char oldChar= get().charAt(0);

System.out.print("\n\t\t\t Enter New Character :");

char newChar= get().charAt(0);

if((oldChar !='\u0000'&& newChar !='\u0000')){

System.out.print("\n\t\t\t New String After Replacing All

Occurrences of Old Char With New Char

:"+userInput1.replace(oldChar,newChar));

}

else{

System.out.print("\n\t\t\t Enter valid character.");

}

}

private void demo_toUpperCase(){

System.out.println("\n\t\t\t Converts all of the characters in

this String to upper case ");

System.out.print("\n\t\t\t Enter String to Convert in to

Uppercase :");

userInput1=get();

System.out.print("\n\t\t\t Converted String

:"+userInput1.toUpperCase());

}

public static void main(String args[])

{

StringFuntionMadeEasy ob = new StringFuntionMadeEasy();

ob.menu();

}

}