• We just launched and are currently in beta. Join us as we build and grow the community.

Telephone Digits Application Program in Java

lowmatild

AI Systems Architect
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
142
Likes
148
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
The following program reads the letter codes ‘A’ through ‘Z’ and prints the corresponding telephone digit. . I will be using the JCreator IDE in developing the program.

To start in this tutorial, first open the JCreator IDE, click new and paste the following code.

  1. import

    javax.swing.JOptionPane

    ;

  2. public

    class

    TelephoneDigits
  3. {
  4. public

    static

    void

    main (

    String

    [

    ]

    args)
  5. {
  6. char

    letter;

  7. String

    inputMessage;
  8. String

    inputString;
  9. String

    outputMessage;

  10. inputMessage =

    "Program to convert uppercase "
  11. +

    "leteers to their corresponding telephone digits.\n

    "
  12. +

    "To stop the program enter #.\n

    "
  13. +

    "ENTER A LETTER: "

    ;

  14. inputString =
  15. JOptionPane

    .showInputDialog

    (

    inputMessage)

    ;

  16. letter =

    inputString.charAt

    (

    0

    )

    ;

  17. while

    (

    letter !=

    '#'

    )
  18. {
  19. outputMessage =

    "The letter you entered is: "

    +

    letter +

    "\n

    "
  20. +

    "The corresponding telephone digit is: "

    ;

  21. if

    (

    letter >=

    'A'

    &&

    letter <=

    'Z'

    )
  22. {
  23. switch

    (

    letter)
  24. {
  25. case

    'A'

    :
  26. case

    'B'

    :
  27. case

    'C'

    :
  28. outputMessage =

    outputMessage +

    "2"

    ;
  29. break

    ;
  30. case

    'D'

    :
  31. case

    'E'

    :
  32. case

    'F'

    :
  33. outputMessage =

    outputMessage +

    "3"

    ;
  34. break

    ;
  35. case

    'G'

    :
  36. case

    'H'

    :
  37. case

    'I'

    :
  38. outputMessage =

    outputMessage +

    "4"

    ;
  39. break

    ;
  40. case

    'J'

    :
  41. case

    'K'

    :
  42. case

    'L'

    :
  43. outputMessage =

    outputMessage +

    "5"

    ;
  44. break

    ;
  45. case

    'M'

    :
  46. case

    'N'

    :
  47. case

    'O'

    :
  48. outputMessage =

    outputMessage +

    "6"

    ;
  49. break

    ;
  50. case

    'P'

    :
  51. case

    'Q'

    :
  52. case

    'R'

    :
  53. case

    'S'

    :
  54. outputMessage =

    outputMessage +

    "7"

    ;
  55. break

    ;
  56. case

    'T'

    :
  57. case

    'U'

    :
  58. case

    'V'

    :
  59. outputMessage =

    outputMessage +

    "8"

    ;
  60. break

    ;
  61. case

    'W'

    :
  62. case

    'X'

    :
  63. case

    'Y'

    :
  64. case

    'Z'

    :
  65. outputMessage =

    outputMessage +

    "9"

    ;

  66. }
  67. }
  68. else
  69. outputMessage =

    outputMessage +

    "INVALID INPUT"

    ;
  70. JOptionPane

    .showMessageDialog

    (

    null

    , outputMessage, "TelephoneDigit"

    ,
  71. JOptionPane

    .PLAIN_MESSAGE

    )

    ;
  72. inputMessage =

    "Enter another uppercase letter to find its correspondinf"
  73. +

    "telephone digit. \n

    "
  74. +

    "To stop the program enter #.\n

    "
  75. +

    "Enter a letter: "

    ;

  76. inputString =
  77. JOptionPane

    .showInputDialog

    (

    inputMessage)

    ;
  78. letter =

    inputString.charAt

    (

    0

    )

    ;
  79. }
  80. System

    .exit

    (

    0

    )

    ;
  81. }
  82. }

Sample Run:

t1_1.png

t2_0.png

t3_0.png


The statement
  1. char

    letter;

  2. String

    inputMessage;
  3. String

    inputString;
  4. String

    outputMessage;

declare the appropriate variables.

The statement
  1. inputMessage =

    "Program to convert uppercase "
  2. +

    "leteers to their corresponding telephone digits.\n

    "
  3. +

    "To stop the program enter #.\n

    "
  4. +

    "ENTER A LETTER: "

    ;

create the appropriate string to be included in the input dialog box.

The expression in the while statement checks that the letter is not #. If the letter entered by the user is not #, the body of the while loop executes.

The if statement checks whether the lettered entered by the user is uppercase.

If the lettered entered is uppercase, the logical expression in the if statements evaluates to true and the switch statement executes, which determines the appropriate telephone digit and appends the telephone digit to output message.

If the letter entered is not uppercase, the else statement executes and appends the string “INVALID INPUT” to output Message.

 

452,292

323,341

323,350

Top