converting words to phonetic spelling
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");
}
}
}
_dragonwolf_
Junior Poster in Training
51 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
This is totally unnecessary:
<strong>stringArray.toString();</strong>
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+" ");
}
}
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
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]+" ");
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
That works perfectly!!! Thank you greatly...
_dragonwolf_
Junior Poster in Training
51 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0