0

I am trying to write a program that will switch any letter of the alphabet (upper or lower cases) AND number into the Phonetic alphabet. For example, if I enter "A" or "a" my program will give me (change it to) "Alpha". Moreover, if I enter "1" it will return "One". I've successfully managed to work the 'enter-any-letter' aspect of it, but my program does not recognize numbers. I tried putting int but my Scanner does not recognize this. I put a default in my code but still...no prevail. Should I use an if statement instead?

Further note: This is question is a continuation from this question

Here's what I've got so far:

import java.util.Scanner;
public class PhoneticTranslate {
public static void main(String[] args) {

 int number = 0;
char letter;
String phonetic = null;

Scanner kb = new Scanner(System.in);


System.out.print("Please enter a letter or number: ");
letter = kb.next().charAt(0);

switch(Character.toUpperCase(letter))
{
case 'A':
    phonetic = "Alpha"; 
break;
case 'B':
    phonetic = "Bravo";
    break;
// ... rest of cases for letters
case 'Z':
    phonetic = "Zulu";
    break;
    default:

            Scanner x = new Scanner(System.in);
            number = kb.nextInt();

            switch(number)
            {
            case '1':
                phonetic = "One";
                break;
            case '2':
                phonetic = "Two";
                break;
                            // ... rest of cases for numbers
            case '8':
                phonetic = "Eight";
                break;
            case '9':
                phonetic = "Nine";
                break;
            }

}
            System.out.println("You Entered " +  letter + ". This letter indicates: " + phonetic);
            System.out.println("You Entered" + number + ". This number indicates: " + phonetic);


}
}
Community
  • 1
  • 1
user1753668
  • 61
  • 1
  • 2
  • 5

9 Answers9

4

A giant switch/case clause is a code smell, try this:

Add every key/value pair into a Map, then you retrieve the values with get. No switch/case needed.

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 "puts" to make it shorter
codes.put("W","Whiskey");
codes.put("X","X-Ray");
codes.put("Y","Yankee");
codes.put("Z","Zulu");
codes.put("0","Zero");
codes.put("1","One");
    // not showing all "puts" to make it shorter
codes.put("9","Nine");    

Scanner kb = new Scanner(System.in);

System.out.print("Please enter a letter: ");
letter = kb.next().toUpperCase(); // convert key to uppercase

phonetic = codes.get(letter);  // search the value in the map using the key

if (phonetic == null) {
    System.out.println("bad code : " + letter);
} else {
    System.out.println("Phonetic: " + phonetic);
}
Tulains Córdova
  • 2,559
  • 2
  • 20
  • 33
3

You have written your cases over characters: -

case '1': // This is checking for character '1'

You need to change your cases to take integer values: -

switch(number) {
    case 1:
           phonetic = "One";
           break;
    case 2:
        ... so on
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • @Rohit Jain This is great! I've done that and it works but when I input an integer number, I have to type it in twice. I think it has something to do with the fact that there are two Scanners. Is there a way so that I don't have to enter a number twice?? – user1753668 Oct 18 '12 at 15:56
  • 1
    @user1753668. Well I would say, you shouldn't have a different switch case for integers. Continue with your previous scanner, and test with '1', '2', so on.. That will be a better way to do.. – Rohit Jain Oct 18 '12 at 15:57
  • @user1753668 But let me see why you are having your current problem. – Rohit Jain Oct 18 '12 at 15:58
  • @user1753668 There you go.. You have defined a scanner variable `x`, but are using `kb` -> `number = kb.nextInt();`.. Change it to: - `number = x.nextInt();` Before your switch-case for integers starts. – Rohit Jain Oct 18 '12 at 15:59
  • @RohitJain I didn't realize the OP was calling `nextInt()`, this sound like the right answer in this case – Ruan Mendes Oct 18 '12 at 16:22
0

Either don't include the quotes around the number (" case 1: phonetic = "One"" etc), or continue using the char value. I think either one should work.

awolfe91
  • 1,627
  • 1
  • 11
  • 11
0

Your switch statement is checking for the unicode char representation of integers. By this specification, '1' is the character "1" which translates to the integer 49.

Put the int representation of each value in the switch statement:

switch (number) {
    case 1:
        phonetic = "One";
        break;
    case 2:
    ...
}
FThompson
  • 28,352
  • 13
  • 60
  • 93
0

Try:

    Scanner x = new Scanner(System.in);
    int number = x.nextInt();
    String phonetic = null;
    switch(number)
    {
    case 1:
        phonetic = "One";
        break;
    case 2:
        phonetic = "Two";
        break;
    case 3:
        phonetic = "Three";
        break;
    case 4:
        phonetic = "Four";
        break;
    case 5:
        phonetic = "Five";
        break;
    case 6:
        phonetic = "Six";
        break;
    case 7:
        phonetic = "Seven";
        break;
    case 8:
        phonetic = "Eight";
        break;
    case 9:
        phonetic = "Nine";
        break;
    }
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

Use the ASCII codes for the numbers instead , that's what chars are anyway. But why do you need to do it? Doesnt your code already work?

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
0

Continue your cases into the characters representing integers:

case 'Z':
    phonetic = "Zulu";
    break;
case '1':
    phonetic = "One";
    break;
case '2':
    // ...

This will work, so long as you only want to handle single-digit numbers.

This matches your problem description, though keeping both a letter and number variable and printing them out separately suggests some additional functionality?

pb2q
  • 58,613
  • 19
  • 146
  • 147
0

You can try to check if the input value is a digit(int). If not return

while(true){

Scanner x = new Scanner(System.in);
int number=0;    
try{
    int number = x.nextInt();
    }catch(IllegalArgumentException e){
    continue;
    }
    String phonetic = null;
    switch(number)
    {
    case 1:
        phonetic = "One";
        break;
    case 2:
        phonetic = "Two";
        break;
    case 3:
        phonetic = "Three";
        break;
    case 4:
        phonetic = "Four";
        break;
    case 5:
        phonetic = "Five";
        break;
    case 6:
        phonetic = "Six";
        break;
    case 7:
        phonetic = "Seven";
        break;
    case 8:
        phonetic = "Eight";
        break;
    case 9:
        phonetic = "Nine";
        break;
    }
}
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
0

Try this one sir

package phone;

import java.util.Scanner;

public class PhoneticTranslate {

 /**
  * @param args
  */
 public static void main(String[] args) {
  int number = 0;
  char letter;
  String phonetic = null;

  Scanner kb = new Scanner(System.in);


  System.out.print("Please enter a letter or number: ");
  letter = kb.next().charAt(0);

  switch (Character.toUpperCase(letter)) {
   case 'A':
    phonetic = "Alpha";
    break;
   case 'B':
    phonetic = "Bravo";
    break;
    // ... rest of cases for letters
   case 'Z':
    phonetic = "Zulu";
    break;
   default:

    Scanner x = new Scanner(System.in);
    number = kb.nextInt();

    switch (number) {
     case 1:
      phonetic = "One";
      break;
     case 2:
      phonetic = "Two";
      break;
      // ... rest of cases for numbers
     case 8:
      phonetic = "Eight";
      break;
     case 9:
      phonetic = "Nine";
      break;
    }

  }
  System.out.println("You Entered " + letter + ". This letter indicates: " + phonetic);
  System.out.println("You Entered" + number + ". This number indicates: " + phonetic);


 }

}
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
varun
  • 1