
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
JAVA Menu Driven Program to Check Character is String, Number or Special Character
In this article, we will see a menu driven program to check if the entered character is number, string or special character by Java programming language. We will be implementing the application using a switch case.
To show you some instances
Instance-1
Suppose the entered character is ‘a’ then the output should be “Entered character is a String”.
Instance-2
Suppose the entered character is ‘1’ then the output should be “Entered character is a number”.
Instance-3
Suppose the entered character is ‘$’ then the output should be “Entered character is a Special character”.
Syntax
To check whether a character is String, Number or a Special character in Java we use isLetter, isDigit or isWhitespace functions. String is checked by isLetter function, Number is checked by isDigit function and special character is checked by combination of isLetter, isDigit and isWhitespace function.
Following is the syntax for String function
Character.isLetter(ob1)
Following is the syntax for number function
Character.isDigit(ob1)
Following is the syntax for String function
(!Character.isDigit(ob1)&& !Character.isLetter(ob1)&& !Character.isWhitespace(ob1))
Algorithm
Step-1 − Ask the user to input the desired character.
Step-2 − Display the menu.
Step-3 − Ask the user to enter their choice.
Step-4 − Use a switch case to go to the choice and perform the operation.
Step-5 − Print the result.
Let’s 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 if a character is a string, number or special character in Java by using a menu driven approach.
- Related Articles
- Java program to check if a string contains any special character
- Java Menu Driven Program to Check Positive Negative or Odd Even Number
- Check input character is alphabet, digit or special character in C
- Java Menu Driven Program to Check Type of a Number
- C# program to check if a string contains any special character
- PHP program to check if a string has a special character
- Program to check if a string contains any special character in C
- Program to check if a string contains any special character in Python
- Java Menu Driven Program to Perform Basic String Operations
- Java Program to Check Whether a Character is Alphabet or Not
- Java program to check occurence of each character in String
- Java program to check occurrence of each character in String
- C# Program to replace a special character from a String
- Java Program to Convert Character to String
- Java Menu Driven Program to Perform Array Operation
