I'm trying to write a program that will switch any letter of the alphabet (upper or lower cases) into the Phontic alphabet. For example, If I enter "A" or "a" my program will give me (change it to) "Alpha". I've done so much research on this and switch statements but I keep getting stuck. I've realized that I can't use 'char' in a scanner. However, when I change 'char' into a 'String' my switch statement messes up (specifically the toUpperCase in my code gets underlined. I can't see my mistake. Here's what I've done so far:
import java.util.Scanner;
public class PhoneticTranslate {
public static void main(String[] args) {
char letter;
String phonetic;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a letter: ");
letter = kb.next();
switch(Character.toUpperCase(letter))
{
case 'A':
phonetic = "Alpha";
break;
case 'B':
phonetic = "Bravo";
break;
case 'C':
phonetic = "Charlie";
break;
case 'D':
phonetic = "Delta";
break;
case 'E':
phonetic = "Echo";
break;
case 'F':
phonetic = "Foxtrot";
break;
case 'G':
phonetic = "Golf";
break;
case 'H':
phonetic = "Hotel";
break;
case 'I':
phonetic = "India";
break;
case 'J':
phonetic = "Juliet";
case 'K':
phonetic = "Kilo";
break;
case 'L':
phonetic = "Lima";
break;
case 'M':
phonetic = "Mike";
break;
case 'N':
phonetic = "November";
break;
case 'O':
phonetic = "Oscar";
break;
case 'P':
phonetic = "Papa";
break;
case 'Q':
phonetic = "Quebec";
break;
case 'R':
phonetic = "Romeo";
break;
case 'S':
phonetic = "Sierra";
break;
case 'T':
phonetic = "Tango";
break;
case 'U':
phonetic = "Uniform";
break;
case 'V':
phonetic = "Victor";
break;
case 'W':
phonetic = "Whiskey";
break;
case 'X':
phonetic = "X-Ray";
break;
case 'Y':
phonetic = "Yankee";
break;
case 'Z':
phonetic = "Zulu";
break;
}
}
}
To take a char input using Scanner and next(), you can use these two lines of code. Scanner input = new Scanner (system.in); char a = input.
The switch statement evaluates an expression, matching the expression's value against a series of case clauses, and executes statements after the first case clause with a matching value, until a break statement is encountered.
Here is the syntax for the Java Scanner class: Scanner input = new Scanner(System.in); int number = input. nextInt(); In this example, we created a variable called input that collects the next value the user inputs into the console.
You need to use charAt. Scanner.next()
method returns String
not char
so you will need to convert String
to char
letter = kb.next().charAt(0);
You can better create a Map<Character, String>
to save yourself from writing 26 cases
in switch. This way you just have to get
the String for a particular character.
Map<Character, String> mapping = new HashMap<Character, String>();
mapping.put('a', "Alpha");
mapping.put('b', "Beta");
.. And so on..
Of course you have to take the burden of initializing the Map
, but it will be better than a Mess
of switch - case
Benefit is that, you can also populate
the Map
from some file
later on.
Then when you read character from scanner, use charAt(0)
to fetch the first character, because Scanner.next()
returns a String: -
letter = kb.next().charAt(0);
// Fetch the Phonetic for this character from `Map`
phonetic = mapping.get(letter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With