Question
hello. i need help with number 2 ONLY

1. Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the teleph
0 0
Add a comment Improve this question Transcribed image text
Answer #1


/*
* The java program that prompts user to enter the letter
* from the console and then using switch case to find the digit corresponding to
* on the telephone and then display the digit corresponding to the letter
* and otherwise print an error message indicating that there
* is not digit corresponding to letter.
* */

//Exercise_2.java
import java.util.Scanner;
public class Exercise_2
{
   public static void main(String[] args)
   {
       //Create a Scanner class object to read input from console
       Scanner kboard=new Scanner(System.in);
       char letter;
       int digit = 0;
       //read letter from console
       System.out.print("Enter a single letter and you will get the corresponding digit on the telephone: ");
       letter=kboard.nextLine().charAt(0);
       /*Switch case to match the case label for letter*/
       switch(letter)
       {
       case 'Q':
       case 'Z':
           //set digit to -1 for Q and Z letters
           digit=-1;
           break;
          
       case 'A':
       case 'B':
       case 'C':
           digit=2;
           break;
          
       case 'D':
       case 'E':
       case 'F':
           digit=3;
           break;
          
       case 'G':
       case 'H':
       case 'I':
           digit=4;
           break;
          
       case 'J':
       case 'K':
       case 'L':
           digit=5;
           break;
       case 'M':
       case 'N':
       case 'O':
           digit=6;
           break;
          
       case 'P':
       case 'R':
       case 'S':
           digit=7;
           break;
          
       case 'T':
       case 'U':
       case 'V':
           digit=8;
           break;
          
       case 'W':
       case 'X':
       case 'Y':
           digit=9;
           break;

       }
       /*Check if digit is -1 the print the error message*/
       if(digit==-1)
           System.out.println("There is no digit on the telephone that corresponds to "+letter);
       else
           //Otherwise print the letter corresponding to the digit
           System.out.println("The digit "+digit+" corresponds to the letter "+letter+" on the telephone.");

   }

}
----------------------------------------------Sample Output------------------------------------------------------

Run#1

Enter a single letter and you will get the corresponding digit on the telephone: Z There is no digit on the telephone that co

Run#2

Enter a single letter and you will get the corresponding digit on the telephone: R the digit 7 corresponds to the letter R on

Add a comment
Know the answer?
Add Answer to:
hello. i need help with number 2 ONLY 1. Use if statements to write a Java...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that inputs a single letter and prints out the corresponding digit on the...

    Write a C++ program that inputs a single letter and prints out the corresponding digit on the telephone.The letters and digits on a telephone are grouped this way:2 = ABC 4 = GHI 6 = MNO 8 = TUV3 = DEF 5 = JKL 7 = PRS 9 = WXYNo digits correspond to either Q or Z. For these two letters, your program should print a messageindicating that they are not used on a telephone. If a letter in lower...

  • Write a program that will calculate your letter grade at the end of the semester for...

    Write a program that will calculate your letter grade at the end of the semester for CSIS130. Your program will read an input file that shall contain all the tests scores and lab scores for the semester, and the input file data should be in the following format: full name of student Test1 Score (0…100), Test2 Score (0..100) , Final Exam Score (0..100), Total Number of Labs: N NLab Scores (0=Fail, 1=Pass) [You will have N lab scores each separated...

  • Please I need help with this c++ code. please show all steps , write comments and...

    Please I need help with this c++ code. please show all steps , write comments and show sample runs. Thank you. 1. The Federal Bureau of Investigation (FBI) has recently changed its Universal Control Numbers (UCN) for identifying individuals who are in the FBI's fingerprint database to an eight-digit base 27 value with a ninth check digit. The digits used are: 0123456789ACDE FHJKLMNPRTVWX Some letters are not used because of possible confusion with other digits: B->8, G->C, I- >1, 0->0,...

  • in java plz amaining Time: 1 hour, 49 minutes, 15 seconds. Jestion Completion Status: Write a...

    in java plz amaining Time: 1 hour, 49 minutes, 15 seconds. Jestion Completion Status: Write a program that asks the user for an input file name, open, read ar number of students is not known. For each student there is a name and and display a letter grade for each student and the average test score fc • openFile: This method open and test the file, if file is not found and exit. Otherwise, return the opened file to the...

  • Input hello, I need help completing my assignment for java,Use the following test data for input...

    Input hello, I need help completing my assignment for java,Use the following test data for input and fill in missing code, and please screenshot output. 1, John Adam, 3, 93, 91, 100, Letter 2, Raymond Woo, 3, 65, 68, 63, Letter 3, Rick Smith, 3, 50, 58, 53, Letter 4, Ray Bartlett, 3, 62, 64, 69, Letter 5, Mary Russell, 3, 93, 90, 98, Letter 6, Andy Wong, 3, 89,88,84, Letter 7, Jay Russell, 3, 71,73,78, Letter 8, Jimmie Wong,...

  • I need help programming this program in C. 1.) Write a program that reads a message,...

    I need help programming this program in C. 1.) Write a program that reads a message, then checks whether it's a palindrome (the letters in the message are the same from left to right as from right to left), example is shown below: Enter a message: He lived as a devil, eh? Palindrome Enter a message: Madam, I am Adam. Not a Palindrome 2.) Ignore all characters that aren't letters. Use integer variables to keep track of positions in the...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • I ONLY need question 2 part C AND D answered...it is in bold. Please do not...

    I ONLY need question 2 part C AND D answered...it is in bold. Please do not use hash sets or tables. In java, please implement the classes below. Each one needs to be created. Each class must be in separate file. The expected output is also in bold. I have also attached the driver for part 4 to help. 1. The Student class should be in the assignment package. a. There should be class variable for first and last names....

  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT