Chapter 4 Problem 15PE Problem The international standard le

Chapter 4, Problem 15PE

Problem

The international standard letter/number mapping found on the telephone is shown below:

Write a program that prompts the user to enter a letter and displays its corresponding number.

I would like to have the solution to the problem stated above.

[Chapter 4, 15PE of the Introduction to Java Programming, Comprehensive Version (10th Edition)].

3

Solution

LetterToNumber.java

import java.util.Scanner;

public class LetterToNumber {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter a letter: \");
       char ch = scan.next().charAt(0);

       if (\"abcABC\".contains(String.valueOf(ch))) {
           System.out.println(\"The corrsponding number is 2\");
       } else if (\"defDEF\".contains(String.valueOf(ch))) {
           System.out.println(\"The corrsponding number is 3\");
       } else if (\"ghiGHI\".contains(String.valueOf(ch))) {
           System.out.println(\"The corrsponding number is 4\");
       } else if (\"jklJKL\".contains(String.valueOf(ch))) {
           System.out.println(\"The corrsponding number is 5\");
       } else if (\"mnoMNO\".contains(String.valueOf(ch))) {
           System.out.println(\"The corrsponding number is 6\");
       } else if (\"pqrsPQRS\".contains(String.valueOf(ch))) {
           System.out.println(\"The corrsponding number is 7\");
       } else if (\"tuvTUV\".contains(String.valueOf(ch))) {
           System.out.println(\"The corrsponding number is 8\");
       } else if (\"wxyzWXYZ\".contains(String.valueOf(ch))) {
           System.out.println(\"The corrsponding number is 9\");
       } else {
           System.out.println(ch + \" is an invalid input\");
       }
   }

}

Output:

Enter a letter: a
The corrsponding number is 2

Enter a letter: g
The corrsponding number is 4

Enter a letter: -
- is an invalid input

Chapter 4, Problem 15PE Problem The international standard letter/number mapping found on the telephone is shown below: Write a program that prompts the user to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site