Cell Phone Keypad Java Program ISC Computer Science 2025 Practical

Most (not all) cell phone keypads look like the following arrangement (the letters are above the respective number):

For sending text/SMS, the common problem is the number of keystrokes to type a particular text.

For example, in the word “STOP”, there are a total of 9 keystrokes needed to type the word. You need to press the key 7 four times, the key 8 once, the key 6 three times and the key 7 once to get it.

Develop a program code to find the number of keystrokes needed to type the text.

For this problem, accept just one word without any punctuation marks, numbers or whitespaces and the text message would consist of just 1 word.

Test your data with the sample data and some random data:

Example 1:
INPUT: DEAR
OUTPUT: Number of keystrokes = 7

Example 2:
INPUT: Thanks
OUTPUT: Number of keystrokes = 12

Example 3:
INPUT: Good-bye
OUTPUT: INVALID ENTRY

import java.util.Scanner;
class Keypad{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the word: ");
        String word = in.next().toUpperCase();
        int count = 0;
        for(int i = 0; i < word.length(); i++){
            char ch = word.charAt(i);
            if(!Character.isLetter(ch)){
                System.out.println("INVALID ENTRY");
                return;
            }
            if("ADGJMPTW".indexOf(ch) >= 0)
                count++;
            else if("BEHKNQUX".indexOf(ch) >= 0)
                count += 2;
            else if("CFILORVY".indexOf(ch) >= 0)
                count += 3;
            else
                count += 4;
        }
        System.out.println("Number of keystrokes = " + count);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *