Home >Java >javaTutorial >JAVA menu driver for checking whether a character is a string, number or special character

JAVA menu driver for checking whether a character is a string, number or special character

WBOY
WBOYforward
2023-09-11 21:49:02899browse

JAVA menu driver for checking whether a character is a string, number or special character

In this article, we will see a menu driver implemented using Java programming language to check whether the entered characters are numbers, strings or special characters. We will use switch case to implement this application.

Show you some examples

Example-1

Suppose the entered character is ‘a’ then the output should be “Entered character is a String”.
The Chinese translation of

Instance-2

is:

Instance-2

Suppose the entered character is ‘1’ then the output should be “Entered character is a number”.
The Chinese translation of

Instance-3

is:

Instance-3

Suppose the entered character is ‘$’ then the output should be “Entered character is a Special character”.

grammar

In Java, we use isLetter, isDigit or isWhitespace function to check whether a character is a string, number or special character. Use the isLetter function to check strings, the isDigit function to check numbers, and a combination of isLetter, isDigit, and isWhitespace functions to check special characters.

The following is the syntax of the string function

Character.isLetter(ob1)

The following is the syntax of numeric functions

Character.isDigit(ob1)

The following is the syntax of the string function

(!Character.isDigit(ob1)&& !Character.isLetter(ob1)&& !Character.isWhitespace(ob1))

algorithm

Step-1 − Ask the user to enter the required characters.

Step 2 - Display the menu.

Step 3 - Ask the user to enter their selections.

Step-4 - Use the switch box to go to the selection and perform the action.

Step 5 - Print the results.

Let us see the program to understand it clearly.

Example

import java.util.*;
public class Main {
   public static void main(String args[]) {
      Scanner sc = new Scanner( System.in );
      System.out.println("Enter a character to check if it's a Number, String or a Special character");
      char ob1 = sc.next().charAt(0);

      System.out.println("Now choose the operation you want to perform from the menu given below. ");

      mainLoop: while (true) {
         Scanner inn = new Scanner( System.in );
         System.out.println("\n***Menu***");
         System.out.println("1. Check if a character is number");
         System.out.println("2. Check if a character is String");
         System.out.println("3. Check if a character is Special character");
         System.out.println("4. Terminate the program");
         System.out.println("Enter action number (1-4): ");
         int command;
         if ( inn.hasNextInt() ) {
            command = inn.nextInt();
            inn.nextLine();
         }
         else {
            System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER.");
            inn.nextLine();
            continue;
         }
         switch(command) {
            case 1:
            if (Character.isDigit(ob1)) {
               System.out.println("Character is a number!");
            } else {
               System.out.println("Character is not a number!");
            }
            break;
            case 2:
            if (Character.isLetter(ob1)) {
               System.out.println("Character is a String!");
            } else {
               System.out.println("Character is not a String!");
            }
            break;
            case 3:
            if (!Character.isDigit(ob1)
            && !Character.isLetter(ob1)
            && !Character.isWhitespace(ob1)) {
               System.out.println("Character is a Special Character!");
            } else {
               System.out.println("Character is not a Special Character!");
            }
            break;
            case 4:
            System.out.println("Program terminated");
            break mainLoop;
            default:
            System.out.println("Wrong choice!!");
         }

      }

   }
}

Output

Enter a character to check if it's a Number, String or a Special character
t
Now choose the operation you want to perform from the menu given below.
***Menu***
1. Check if a character is number
2. Check if a character is String
3. Check if a character is Special character
4. Terminate the program
Enter action number (1-4):
1
Character is not a number!
***Menu***
1. Check if a character is number
2. Check if a character is String
3. Check if a character is Special character
4. Terminate the program
Enter action number (1-4):
3
Character is not a Special Character!
***Menu***
1. Check if a character is number
2. Check if a character is String
3. Check if a character is Special character
4. Terminate the program
Enter action number (1-4):
2
Character is a String!
***Menu***
1. Check if a character is number
2. Check if a character is String
3. Check if a character is Special character
4. Terminate the program
Enter action number (1-4):
$
ILLEGAL RESPONSE. YOU MUST ENTER A NUMBER.
***Menu***
1. Check if a character is number
2. Check if a character is String
3. Check if a character is Special character
4. Terminate the program
Enter action number (1-4):
1
Character is not a number!
***Menu***
1. Check if a character is number
2. Check if a character is String
3. Check if a character is Special character
4. Terminate the program
Enter action number (1-4):
3
Character is not a Special Character!
***Menu***
1. Check if a character is number
2. Check if a character is String
3. Check if a character is Special character
4. Terminate the program
Enter action number (1-4):
2
Character is a String!
***Menu***
1. Check if a character is number
2. Check if a character is String
3. Check if a character is Special character
4. Terminate the program
Enter action number (1-4):
4
Program terminated

In this article, we explored how to check whether a character is a string, number, or special character in Java by using a menu-driven approach.

The above is the detailed content of JAVA menu driver for checking whether a character is a string, number or special character. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete