Write a program that: Asks the user to enter a word. If word starts with a vowel
ID: 3792278 • Letter: W
Question
Write a program that: Asks the user to enter a word. If word starts with a vowel, the program prints that it starts with a vowel. If word starts with a consonant, the program prints that it starts with a consonant. If word starts with neither a vowel nor a consonant (e.g., it starts with a number, or a punctuation character, or it is the empty string), the program prints that it starts with neither a vowel nor a consonant. Example Program Run: Please enter a word: 543 543 starts with neither a vowel nor a consonant. Example Program Run: Please enter a word: Airplane Airplane starts with a vowel. Example Program Run: Please enter a word: cat cat starts with a consonant.Explanation / Answer
import java.util.Scanner;
public class WordStart{
public static void main(String []args) {
Scanner scanner = new Scanner(System.in);
String inputWord, message;
System.out.print("Please enter a word: ");
inputWord = scanner.nextLine();
message = inputWord + " ";
if(inputWord.toLowerCase().charAt(0) >= 'a'
&& inputWord.toLowerCase().charAt(0) <= 'z'){
switch(inputWord.toLowerCase().charAt(0)){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
message += "starts with a vowel.";
break;
default:
message += "starts with a consonant.";
}
}
else {
message += "starts with neither a vowel nor a consonant.";
}
System.out.print(message);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.