Java Program to Detect the Number Key Pressed

In this article we will see how you can detect which number key pressed by suing Java programming language.

Java Program to Detect the Number Key Pressed

As per the problem statement you will allow the user to enter any key and you have to detect that the pressed key is which digit. If user has pressed number keys from 0 to 9 then you have to detect that which number key has been pressed else if any other character key has been pressed then you have to tell ‘What you have entered that is not allowed’. If the user has entered more than one character then tell the user ‘You have entered more than one character’.

Let’s understand it with example.

User has pressed 8 key.
Output: You have pressed 8

User has pressed w key.
Output: What you have entered that is not allowed

User has pressed 5 key & 6 key  means 56
Output: You have entered more than one character

Let’s see the program to understand it more clearly.

Approach:

  • Declare an String variable say str.
  • Ask the user to enter any character (mostly ask to enter any digit).
  • Check if the length of user input value is more than one then print “You have entered more than one character” and return.
  • Else convert the String value to Character value and store it in an char type say ch.
  • Check the character is a digit or not by using isDigit() method of Character class.
  • If that character is a digit then print that number key has been pressed.
  • Else print ‘What you have entered that is not allowed’.

Program:

import java.lang.*;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //ask the user to enter any digit
        System.out.println("Enter any digit:  ");
        //assigning the user input digit to a String variable
        String str = sc.next();
        
        //Checking the length of input String
        //If length is more than 1 that means you have entered more than one character
        if (str.length() >1)
        {
            System.out.println("You have entered more than one character"); 
            return;
        }
        
        //converting the String into an character
        char ch=str.charAt(0);
        
        //Checking the character is a digit or not
        boolean result = Character.isDigit(ch);
        
        //if input value is a digit then print the input value
        if(result) 
            System.out.println("You have pressed "+ch);
        //else print Not Allowed 
        else
            System.out.println("What you have entered that is not allowed");
    }
}

Output:

Case-1
Enter any digit: 
6
You have pressed 6

Case-2
Enter any digit: 
h
What you have entered that is not allowed

Case-3
Enter any digit: 
68
You have entered more than one character

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.