Java Menu Driven Program for String Operations

In this article we are going to see a menu driven string application by using Java program. Menu driven program in java using switch case example, Menu driven program in java using methods, Menu driven program for array operations in java, Menu driven program in java using if else, Menu driven program in java using scanner class, Menu driven program in java using do while, Menu-driven program in java.

Also Read: Java Menu Driven Program to Perform Basic Operations on Two Matrices

Java Menu Driven Program for String Operations

We will design a menu driven program for 4 String operations. Like this you can add more String operations as well.

  1. String Length
  2. Concatenating two strings
  3. Compare two strings
  4. Trim a string

User Defined Methods Used:

  • len( ) – To find the length of the string
  • concatenate( ) – Add two strings
  • compareStr( ) – Compare two strings
  • trimStr( ) – Trim whitespaces

Approach:

  • In Driver method, take a while loop display all the methods.
  • Ask the user to enter the choice.
  • Use switch case for navigation.
  • To find length of a string we will call the user defined method len( ) function. This function takes the string as input and then using inbuilt length() method of the string library to print the length of the string period.
  • To concatenate two strings we will call the user defined method concatenate( ) which asks the user to input two strings and then concatenate strings(using inbuilt concat() library method) and store it in another variable.
  • To compare two strings, call user defined method compareStr( ) which compares two Strings by using inbuilt equals() method and returns a True if both String matches else returns False value.
  • To trim a string call user defined trimStr( ). This method takes in one string and then uses the string library trim() method to trim the whitespaces from the string.

Program:

import java.util.*;
import java.lang.*;

public class Main
{
    static String s,s1,s2;
    static Scanner sc = new Scanner(System.in); 
    
    //Find length of a String
    static void len()
    {
        sc.nextLine();
        System.out.println("Enter the string");
        s=sc.nextLine();
        System.out.println("The string length is "+ s.length());
    }

    //Concatenate two Strings
    static void concatenate()
    {
        sc.nextLine();
        System.out.println("Enter First string to concat");
        s = sc.nextLine();
        System.out.println("Enter Second string to concat ");
        s1 = sc.nextLine();
        // Concatenating string s and s1 and storing in s2
        s2 = s.concat(s1);
        System.out.println("The concatenated string of "+s+" and "+s1+" is "+s2);
    }
    
    //Compare two Strings
    static void compareStr()
    {
        sc.nextLine();
        System.out.println("Enter First string to compare");
        s = sc.nextLine();
        System.out.println("Enter Second string to compare");
        s1 = sc.nextLine();
        // b stores the comparison result
        boolean b = s.equals(s1);
        if(b)
            System.out.println("The strings "+s+" and "+s1+ " are equal.");
        else
            System.out.println("The strings "+s+" and "+s1+ " are not equal.");
    }
    
    //Trimming spaces from String
    static void trimStr()
    {
        sc.nextLine();
        System.out.println("Enter string to trim");
        s=sc.nextLine();
        // Storing the trimmed string in s1
        s1= s.trim();
        System.out.println("The string "+s+" after trimming is "+s1);
    }
    
    //Driver method
    public static void main(String []args)
    {
        int ch; 
        while(true)
        {
            System.out.println("---\t String Operations\t---");
            System.out.println("1: Find the length Of String");
            System.out.println("2: Concatenate two strings");
            System.out.println("3: Compare two strings");
            System.out.println("4: String Trimming");
            System.out.println("5: Quit");
            System.out.print("Enter your choice: ");  
            ch = sc.nextInt();
            switch (ch)
            {
                case 1:len();
                    System.out.println("\n");
                    break;

                case 2:concatenate();
                    System.out.println("\n");
                    break;

                case 3:compareStr();
                    System.out.println("\n");
                    break;

                case 4: trimStr();
                    System.out.println("\n");
                    break;

                case 5:
                    System.exit(0);

                default: System.out.println("Invalid choice! Retry");
            }
        }

    }
}

Output:

--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 1
Enter the string
BtechGeeks
The string length is 10


--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 3
Enter First string to compare
Exception
Enter Second string to compare
Error
The strings Exception and Error are not equal.


--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 2
Enter First string to concat
Btech
Enter Second string to concat 
Geeks
The concatenated string of Btech and Geeks is BtechGeeks


--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 4
Enter string to trim
BtechGeeksBest
The string BtechGeeksBest after trimming is BtechGeeksBest


--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 4
Enter string to trim
India 
The string India after trimming is India


--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 5

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Test Yourself Menu Driven Program In Java Questions:

  1. Menu driven program in java using methods?
  2. Menu driven program in java for string operations?
  3. Java string program examples?
  4. Java program to perform string operations?
  5. String operations in java program?
  6. How to do string programs in java?
  7. How to do menu driven program in java?
  8. How to create menu driven program in java?