In Java, we can check if a character is a digit using either an if-else condition or a ternary operator. This article focuses on explaining this concept through examples, an algorithm, detailed logic, and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements
- Ternary Operator
Problem Statement
We need to implement a Java program to check whether a given character is a digit or not. The program should take a character as input and output whether it is a digit or not. This above action should be done in our program. Let’s look at an example,
Example 1:
Input: 9 Output: Given input is a digit
Here, for the input 9, Output is “Given input is a digit” as 9 is a number.
Example 2:
Input: A Output: Given input is not a digit
Here, for the input A, Output is “Given input is not a digit” as A is an alphabet.
Algorithm to Check Character is Digit or not
- Start: Begin the algorithm with input character ch
- Characters take only one character at one time
- We use relational operators (>= and <=) .
- Condition check: We will take digits from Zero to Nine
- When the character is greater than or equal to zero, and less than or equal to nine then we say, the given input is digit
- Else, we say the given input is not digit.
- End: The algorithm terminates with the result of the condition check.
Logic to Check Character is Digit or Not
There are multiple logics to check a given character is digit or not in Java Programing language.
Logic 1: Using if-else
- Take an input value (char type) from user
- In Scanner class, we don’t have a method to read char type,so use
- char ch=scanner.next().charAt(0)
- This will gives us character value at index 0. As char can store one value at a time, we can read values from user.
- char ch=scanner.next().charAt(0)
- Read and Store the value
- If character greater than or equal to 0 and less than or equal to 9, then we say it is digit otherwise not.
if (ch>= '0' && ch<='9')
System.out.println("Given input character is digit");
else
System.out.println(ch+"is not a digit");
Logic 2: Ternary Operator
- This operator takes three operands and works similar to the if else statements. If condition true, it prints first expression and if condition becomes false then it prints second expression.
- When character greater than or equal to 0 and less than or equal to 9, then we say it is digit otherwise not.
result=(ch>='0' && ch<='9')? ch+" is digit":ch+" is not digit";
System.out.println(result);
Java Program to check character is a digit or not
Program 1: Using if-else
In the below program we are using if else to check character is a digit or not in Java.
import java.util.Scanner;
public class Digit {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Enter a character");
char ch = s.next().charAt(0);
if(ch>='0' && ch<='9')
System.out.println("Given character is a digit");
else
System.out.println("Given character is not a digit");
s.close();
}
}
Output:
Enter a character
5
Given character is a digit
Program Explanation to check character is a digit or not
- The program imports the Scanner class to facilitate user input.
- Defines a class named Digit.
- In the main method:
- Creates a Scanner object ‘s’ to read user input.
- Asks the user to enter a character.
- Reads the input character using the next() method and retrieves the first character using charAt(0).
- Checks if the character is within the range of ‘0’ to ‘9’, inclusive, using ASCII values.
- If the character is within this range, it prints “Given character is a digit”.
- If the character is not within this range, it prints “Given character is not a digit”.
- Closes the Scanner object to release system resources.
Program 2: Using Ternary Operator
In the below program we are using ternary operator to check if a character is digit or not in Java.
import java.util.Scanner;
public class DigitTernaryOperator {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Enter a character");
char ch = s.next().charAt(0);
String result;
result =(ch>='0' && ch<='9')?
ch+" is a digit":
ch+" is not a digit";
System.out.println(result);
s.close();
}
}
Output:
Enter a character
1
1 is a digit
Program Explanation to check character is a digit or not
- The program imports the Scanner class to facilitate user input.
- Defines a class named DigitTernaryOperator.
- In the main method:
- Creates a Scanner object ‘s’ to read user input.
- Asks the user to enter a character.
- Reads the input character using the next() method and retrieves the first character using charAt(0).
- Uses a ternary operator to assign the appropriate message to the variable ‘result’ based on whether the character is a digit or not.
- Prints the ‘result’ message.
- Closes the Scanner object to release system resources.
Conclusion
In this article, we explored how to check if a character is digit or not using if-else statement and the ternary operator. We understood the basic logic to achieve this and moved on to writing the Java program.
For more information on related topics, you can find these articles which might be helpful:
- Understanding the Conditional statements in Java
- Concepts of Operators in Java
- Top 10 Java programming tips
We hope this tutorial has been helpful. If you have any questions or need any further clarification, feel free to comment below. Don’t forget to subscribe to our newsletter for more Java Programming tips and tutorials!
Happy Learning!