I am having an issue converting words into phonetic spellings. Example: userInput = apple / outResult = Alpha Papa Papa Lima Echo

I can get my code to convert a single letter to its respective phonetic. I can also get it to split a string into separate characters.

Here is what I have so far....
If a string is entered it will display it separated....
(i.e. apple = a p p l e)
If a single letter is entered....
(i.e. a = Alpha)

I only have letters 'a', 'b', and 'c'...simple reason -- if I can get it to change 'cab' to 'Charlie Alpha Bravo' then I can complete the rest....

on another note I can use a switch statement if possible....not sure how to set that up though.....

I appreciate the help..........

package phonetic;

import javax.swing.JOptionPane;

public class phonetic {

    public static void main(String[] args)
    {
        phoneticAlpha();
    }

    private static void phoneticAlpha()
    {
        String userInput;
        userInput = JOptionPane.showInputDialog("Enter a word");

        char []stringArray;
        stringArray = userInput.toCharArray();

        for(int i = 0; i < stringArray.length; i++)
            System.out.print(stringArray[i] + " ");    System.out.print("\n");

        if(userInput.equalsIgnoreCase("a"))
        {
            stringArray.toString();
            System.out.println("Alpha");
        }
        if(userInput.equalsIgnoreCase("b"))
        {
            stringArray.toString();
            System.out.println("Bravo");
        }
        if(userInput.equalsIgnoreCase("c"))
        {
            stringArray.toString();
            System.out.println("Charlie");
        }
    }

}

Recommended Answers

All 3 Replies

This is totally unnecessary:

[B]stringArray.toString();[/B]
System.out.println("Alpha");

It doesn't do anything. You don't need it. And put the if statements in the for loop:

for(int i = 0; i < stringArray.length; i++) {
  char ch = stringArray[i];

  if  ((ch=='a')||(ch=='A')) {
     System.out.print("Alpha ");
  } else if ((ch=='b')||(ch=='B')) {

   } else if (...) {..
....
   } else {
      // IN CASE IT IS NOT A LETTER
     System.out.print(ch+" ");
   }
}

With switch:

for(int i = 0; i < stringArray.length; i++) {
  char ch = stringArray[i];
  
  switch (ch) {
     case 'a':
     case 'A':
       System.out.print("Alpha ");
       break;
     case 'b':
     case 'B':
       System.out.print("Bravo ");
       break;
     ....
....
...
    default:
       System.out.print(ch+" ");
  }
}

Sorry for my double post but when you are done with your solution, there is a much smarter - but more difficult - way you can try:

String [] words = {"Alpha", "Bravo", ....};

Now when you get each character you can use its ASCII equivalent, then normalize that value to be between 0 to 25 and use it as index. In that way you will not have to write entire code with if statements:

char ch = stringArray[i];
int index = // convert ch into an int and normalize
System.out.print(words[index]+" ");
commented: Thank you greatly +1

That works perfectly!!! Thank you greatly...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.