| | |
converting words to phonetic spelling
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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..........
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)
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"); } } }
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
0
#2 Oct 16th, 2009
This is totally unnecessary:
It doesn't do anything. You don't need it. And put the if statements in the for loop:
With switch:
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)
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:
Java Syntax (Toggle Plain Text)
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+" "); } }
Check out my New Bike at my Public Profile at the "About Me" tab
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:
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)
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)
char ch = stringArray[i]; int index = // convert ch into an int and normalize System.out.print(words[index]+" ");
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Which are the Best PPC Program? (Pay-Per-Click Advertising)
- Array and data type help (C++)
- converting numbers to words (VB.NET)
- Curse words - what are they really? (Geeks' Lounge)
- Splitting words from string into character array (C++)
- DrScheme - Convert a string into a list of words (Computer Science)
- Finding the first number char in a string (Java)
- how to validate checkbox with different names? (HTML and CSS)
Other Threads in the Java Forum
- Previous Thread: jdk version
- Next Thread: some help please...
| Thread Tools | Search this Thread |
3d @param affinetransform android api applet application arc arguments array arrays automation binary bluetooth byte capture chat class classes click client code color compare component count database design detection eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong print problem producer program programming project projectideas read recursion replaysolutions rim scanner screen server set size sms sort sql string swing terminal threads transforms tree ui web windows






