Question

In: Computer Science

Write Java program that asks a user to input a letter, converts the user input to...

Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered.

For letters A or B or C display 2

For letter D or E or F display 3

For letter G or H or I display 4

For letter J or K or L display 5

For letter M or N or O display 6

For letter P or Q or R or S display 7

For letter T or U or V display 8

For W or X or Y or Z display 9

Use a switch statement in this program

Sample input and output would be user entering a small letter a and the output being:

"The letter you entered was A"

"The number associated with the letter you entered is 2"

Another example of input and output would be the used entering a small m and the output being:

"The letter you entered was M"

The number associated with the letter you entered is 6"

Solutions

Expert Solution

The below code gives the result as oer the requirement given in the question.

The code has been well commented to understand the logic.

Code:

import java.util.*;
public class Main {
    public static void main(String args[]) {
      String letter;
                System.out.println("Enter a letter: ");
                
                Scanner sc = new Scanner(System.in);  // craete a Scanner class object to take input from user
                letter = sc.nextLine();  // store the input in the variable
                System.out.println("The letter you entered was "+ letter.toUpperCase() );  // string.toUpperCase() to convert into upper case
                
                switch(letter.toLowerCase() ){  // the switch statement to generate the number associated with the letter
                    case "a":  // multiple condition same result type of case statement
                    case "b":
                    case "c":
                        System.out.println("The number associated with the letter you entered is 2" ); // print statement for each case to generate the associated number
                        break;
                    case "d":
                    case "e":
                    case "f":
                        System.out.println("The number associated with the letter you entered is 3" );
                        break;
                    case "g":
                    case "h":
                    case "i":
                        System.out.println("The number associated with the letter you entered is 4" );
                        break;
                    case "j":
                    case "k":
                    case "l":
                        System.out.println("The number associated with the letter you entered is 5" );
                        break;
                    case "m":
                    case "n":
                    case "o":
                        System.out.println("The number associated with the letter you entered is 6" );
                        break;
                    case "p":
                    case "q":
                    case "r":
                    case "s":
                        System.out.println("The number associated with the letter you entered is 7" );
                        break;
                    case "t":
                    case "u":
                    case "v":
                        System.out.println("The number associated with the letter you entered is 8" );
                        break;
                    case "w":
                    case "x":
                    case "y":
                    case "z":
                        System.out.println("The number associated with the letter you entered is 9" );
                        break;
                }
    }
}

Output Screenshot :


Related Solutions

1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
IN JAVA Create a program that asks the user to input the day, high and low...
IN JAVA Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:                         Day: (variable)                         High: (variable)                         Count High: (variable)                         Total High: (variable)                         Average High: (variable) After the seven days and highs...
IN JAVA Create a program that asks the user to input the day, high and low...
IN JAVA Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:                         Day: (variable)                         High: (variable)                         Count High: (variable)                         Total High: (variable)                         Average High: (variable) After the seven days and highs...
Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements and create an array. Then, write a Merge Sort function with recursion (in the main) that takes the user inputted elements in the array, sorts them, and prints them back.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT