How can I make this switch statement work with a scanner?

I am trying to write a program that will switch any letter of the alphabet (upper or lower case) to the Phontic alphabet. For example, if I enter "A" or "a", my program will give me (change it) to "Alpha". I have done so much research about this and switched statements, but I keep getting stuck. I realized that I can not use "char" in the scanner. However, when I change 'char' to 'String', my switch statement gets confused (in particular, toUpperCase is underlined in my code. I do not see my error. Here's what I have 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; } } } 
+6
source share
4 answers

You need to use charAt. Scanner.next() returns String not char , so you will need to convert String to char

 letter = kb.next().charAt(0); 
+6
source

You can better create a Map<Character, String> to save yourself from writing 26 cases on the switch. So you just need to get String for a specific character.

 Map<Character, String> mapping = new HashMap<Character, String>(); mapping.put('a', "Alpha"); mapping.put('b', "Beta"); .. And so on.. 

Of course, you must take the burden of initializing Map , but it will be better than a Mess of switch - case

The advantage is that you can also populate Map from some file .

Then, when you read the character from the scanner, use charAt(0) to extract the first character, because Scanner.next() returns the string: -

 letter = kb.next().charAt(0); // Fetch the Phonetic for this character from `Map` phonetic = mapping.get(letter); 
+3
source
 String letter; String phonetic; Map<String,String> codes = new HashMap<String,String>(); codes.put("A","Alpha"); codes.put("B","Bravo"); codes.put("C","Charlie"); codes.put("D","Delta"); // not showing all assignments to make it shorter codes.put("W","Whiskey"); codes.put("X","X-Ray"); codes.put("Y","Yankee"); codes.put("Z","Zulu"); Scanner kb = new Scanner(System.in); System.out.print("Please enter a letter: "); letter = kb.next().toUpperCase(); phonetic = codes.get(letter); if (phonetic == null) { System.out.println("bad code : " + letter); } else { System.out.println("Phonetic: " + phonetic); } 
+1
source

Source: https://habr.com/ru/post/927956/


All Articles