
-----------------------------------
helpneeded
Mon Jan 02, 2006 9:47 am

Converting from morse into english
-----------------------------------
I have to write a program that converts long sentenses to morse code and from morse code to english

can someone give me a little hint on how to start, please!

-----------------------------------
rizzix
Mon Jan 02, 2006 11:30 am


-----------------------------------
Create a HashTable that maps morse code to english alphabets or visa-versa. ;)

-----------------------------------
helpneeded
Mon Jan 02, 2006 1:42 pm

hmm
-----------------------------------
I dunno how to use that, i was thinking about using arrays

bare with me for a second

------
the user would be asked to input the message
please enter message: [input]           --- input would be the message (and the stuff to be translated)

is it possible to convert the message [input] into arrays so that each string within the array can be accessed individualy ?

------------

for example; if the user enters "Hello I would like to convert this text into morse code"

is it possible to have access to hello, i, would, like, to, etc... (Or whatever the string within a random input is}?

-----------------------------------
turboliux
Mon Jan 02, 2006 2:22 pm


-----------------------------------
i would make that first the user chooses:
1. Morse > English
2. English > Morse

and then use one of the following methods:
toMorse(), toEnglish()

as rizzix said, it better to use hash tables, because letters in Morse are not equal in length.

-----------------------------------
wtd
Mon Jan 02, 2006 2:23 pm


-----------------------------------
Take this as an opportunity to learn how to use hash tables.

Google for "hashtable site:java.sun.com"

-----------------------------------
turboliux
Mon Jan 02, 2006 2:29 pm


-----------------------------------
for toMorse() i would use following aproach:
switch (letter){
case 'a':
array1 [0] = 96;
array1 [1] = 95;
break;

case 'b':
array2 [0] = 95;
array2 [1] = 96;
array2 [2] = 96;
array2 [3] = 96;
break;
}

-----------------------------------
wtd
Mon Jan 02, 2006 2:38 pm


-----------------------------------
Don't use magic numbers.  Create constants like:

final int DASH = ...;
final int DOT = ...;

-----------------------------------
helpneeded
Mon Jan 02, 2006 3:34 pm


-----------------------------------
Thanks, but it is required that i use arrays, is there a way to do it with arrays, it doesnt matter if its harder, i want to get a good mark on this!

-----------------------------------
wtd
Mon Jan 02, 2006 6:00 pm


-----------------------------------
Well, do you understand ASCII?

-----------------------------------
helpneeded
Mon Jan 02, 2006 8:30 pm


-----------------------------------
Yep, I know ASCII

Ok, I got the basic part of the english to morse (just BASIC) processing, it is functioning; however, I cannot use the same concept I used to translate English to Morse for the translation from Morse to English due to the difference in sizes 


HERE::: 

// The "ISP" class. 
import java.awt.*; 
import hsa.Console; 
import java.io.*; 

public class ISP 
{ 
static Console c; // The output console 
static int x = 0; 
static String content; 
static PrintWriter output; 
static int size; 
static String arrayName[] = new String [9999999]; //Array declaration 
public static void askData () 
{ 
c.print ("Enter: "); 
content = c.readLine (); 
size = content.length (); 

} 



private static String translate (String content, int size) 
{ 
while (x < size) 
{ 
if (content.charAt (x) == 'a') 
return (".-"); 
else if (content.charAt (x) == 'b') 
return ("-..."); 
else if (content.charAt (x) == 'c') 
return ("-.-."); 
else if (content.charAt (x) == 'd') 
return ("-.."); 
else if (content.charAt (x) == 'e') 
return ("."); 
else if (content.charAt (x) == 'f') 
return ("..-."); 
else if (content.charAt (x) == 'g') 
return ("--."); 
else if (content.charAt (x) == 'h') 
return ("...."); 
else if (content.charAt (x) == 'i') 
return (".."); 
else if (content.charAt (x) == 'j') 
return (".---"); 
else if (content.charAt (x) == 'k') 
return ("-.-"); 
else if (content.charAt (x) == 'l') 
return (".-.."); 
else if (content.charAt (x) == 'm') 
return ("--"); 
else if (content.charAt (x) == 'n') 
return ("-."); 
else if (content.charAt (x) == 'o') 
return ("---"); 
else if (content.charAt (x) == 'p') 
return (".--."); 
else if (content.charAt (x) == 'q') 
return ("--.-"); 
else if (content.charAt (x) == 'r') 
return (".-."); 
else if (content.charAt (x) == 's') 
return ("..."); 
else if (content.charAt (x) == 't') 
return ("-"); 
else if (content.charAt (x) == 'u') 
return ("..-"); 
else if (content.charAt (x) == 'v') 
return ("...-"); 
else if (content.charAt (x) == 'w') 
return (".--"); 
else if (content.charAt (x) == 'x') 
return ("-..-"); 
else if (content.charAt (x) == 'y') 
return ("-.--"); 
else if (content.charAt (x) == 'z') 
return ("--.."); 
else if (content.charAt (x) == '1') 
return (".----"); 
else if (content.charAt (x) == '2') 
return ("..---"); 
else if (content.charAt (x) == '3') 
return ("...--"); 
else if (content.charAt (x) == '4') 
return ("....-"); 
else if (content.charAt (x) == '5') 
return ("....."); 
else if (content.charAt (x) == '6') 
return ("-...."); 
else if (content.charAt (x) == '7') 
return ("--..."); 
else if (content.charAt (x) == '8') 
return ("---.."); 
else if (content.charAt (x) == '9') 
return ("----."); 
else if (content.charAt (x) == '0') 
return ("-----"); 
else if (content.charAt (x) == ' ') //Spacing 
return ("/"); 
else 
return ("WRONG"); 
} 
return (""); 
} 



public static void displayData () // Displays the data in an array (English to Morse) 
{ 
while (true) 
{ 
arrayName [x] = translate2 (content, size); 
c.print (arrayName [x]); 
c.print (" "); 
x = x + 1; 
} 
} 


//////////////////////////////////Morse to English\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 
private static String translate2 (String content, int size) 
{ 
while (true) 
{ 
if (content.charAt (x) == '.' && content.charAt (x + 1) == '-' && content.charAt (x + 2) == ' ') 
return ("A"); 

else if (content.charAt (x) == '-' && content.charAt (x + 1) == '.' && content.charAt (x + 2) == '.' && content.charAt (x + 3) == ' ') 
return ("B"); 

} 
} 


public static void displayData2 () // Displays the data in an array (Morse to English) 
{ 
while (content.charAt (x) != '/') 
{ 
arrayName [x] = translate2 (content, size); 
c.print (arrayName [x]); 
c.print (" "); 
x = x + 1; 
} 
} 


public static void main (String[] args) 
{ 
c = new Console (); 
askData (); 
displayData2 (); 

// Place your program here. 'c' is the output console 
} // main method 
} // ISP class 


ASCII would have helped in translating from English to Morse, I am not sure if it would help in translating from Morse to English; 

is there a way? (Thinking)

-----------------------------------
wtd
Mon Jan 02, 2006 10:18 pm


-----------------------------------
There are several issues with the code you've shown us.  


Use code tags!
You do not need to manually keep track of the size of Strings and arrays in Java.

String content, int size
static String arrayName[] = new String [9999999];

Why have you allocated such an enormous array?  This is eating a huge chunk of memory.
Your long "if...else if... else" can be much better expressed as a "switch" construct.


-----------------------------------
Andy
Thu Jan 05, 2006 11:33 am


-----------------------------------
store your morse code in an array of string, then use indexOf on each character from your english word.
