converting words to phonetic spelling

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 47
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster

converting words to phonetic spelling

 
0
  #1
Oct 16th, 2009
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..........
  1. package phonetic;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class phonetic {
  6.  
  7. public static void main(String[] args)
  8. {
  9. phoneticAlpha();
  10. }
  11.  
  12. private static void phoneticAlpha()
  13. {
  14. String userInput;
  15. userInput = JOptionPane.showInputDialog("Enter a word");
  16.  
  17. char []stringArray;
  18. stringArray = userInput.toCharArray();
  19.  
  20. for(int i = 0; i < stringArray.length; i++)
  21. System.out.print(stringArray[i] + " "); System.out.print("\n");
  22.  
  23. if(userInput.equalsIgnoreCase("a"))
  24. {
  25. stringArray.toString();
  26. System.out.println("Alpha");
  27. }
  28. if(userInput.equalsIgnoreCase("b"))
  29. {
  30. stringArray.toString();
  31. System.out.println("Bravo");
  32. }
  33. if(userInput.equalsIgnoreCase("c"))
  34. {
  35. stringArray.toString();
  36. System.out.println("Charlie");
  37. }
  38. }
  39.  
  40. }
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso
 
0
  #2
Oct 16th, 2009
This is totally unnecessary:
stringArray.toString();
System.out.println("Alpha");

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

  1. for(int i = 0; i < stringArray.length; i++) {
  2. char ch = stringArray[i];
  3.  
  4. if ((ch=='a')||(ch=='A')) {
  5. System.out.print("Alpha ");
  6. } else if ((ch=='b')||(ch=='B')) {
  7.  
  8. } else if (...) {..
  9. ....
  10. } else {
  11. // IN CASE IT IS NOT A LETTER
  12. System.out.print(ch+" ");
  13. }
  14. }

With switch:
  1. for(int i = 0; i < stringArray.length; i++) {
  2. char ch = stringArray[i];
  3.  
  4. switch (ch) {
  5. case 'a':
  6. case 'A':
  7. System.out.print("Alpha ");
  8. break;
  9. case 'b':
  10. case 'B':
  11. System.out.print("Bravo ");
  12. break;
  13. ....
  14. ....
  15. ...
  16. default:
  17. System.out.print(ch+" ");
  18. }
  19. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso
 
1
  #3
Oct 16th, 2009
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:
  1. 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:

  1. char ch = stringArray[i];
  2. int index = // convert ch into an int and normalize
  3. System.out.print(words[index]+" ");
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 47
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster
 
0
  #4
Oct 16th, 2009
That works perfectly!!! Thank you greatly...
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC