944,199 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4216
  • Java RSS
Oct 16th, 2009
0

converting words to phonetic spelling

Expand Post »
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..........
Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
_dragonwolf_ is offline Offline
51 posts
since Jul 2009
Oct 16th, 2009
0
Re: converting words to phonetic spelling
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:

Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 16th, 2009
1
Re: converting words to phonetic spelling
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:
Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  1. char ch = stringArray[i];
  2. int index = // convert ch into an int and normalize
  3. System.out.print(words[index]+" ");
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 16th, 2009
0
Re: converting words to phonetic spelling
That works perfectly!!! Thank you greatly...
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
_dragonwolf_ is offline Offline
51 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: jdk version
Next Thread in Java Forum Timeline: some help please...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC