Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Morse code conversion

 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For class me have to make a program that will convert text to morsecode and vice versa. I see how to do it but i can't figure it up in code. What i have so far is a 3 classes: Encode(text to morse), Decode(Morse to Text), and Converter which contains the Main method and takes the input. What i did was make a file that contatins the ABCs and 0-9 and put the conversions next to them seperated by a space. What i need to do is get the input, convert each letter, and then put them back. Im not sure what to use to do this. If anyone could give me some ways to do it that would help greatly. Im not asking for the whole code, just a good starting point.
-Clay
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a HashMap that contains letters and code.
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know what that is but i read some of the APi on Strings and i used toCharArray which will work with Text to Morse but Morse to Text i will need something different but one thing at a time.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Hashmap is a structure that maps keys to values. So you could have one map where the keys are the characters, and the values are the codes, and another where the keys are the codes, and the values are the letters.
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds exactly what i needed. I'll look into it. Thanks for the help so far. Ill post here when i get a basic structure down.
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A question with HashMaps. It compiles fine pretty much but it tells me run with the compiler with -Xlint(im using JCreator) so i do it and it gives me a warning for every key that i added. It says: [unchecked] unchecked call to put(k,v) as a member of the raw type java.util.HashMap. What does that mean and should i just ignore it?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read up on generics. You probably used code like

Under java 1.5/5.0/Tiger this should be coded as


The older code construct is still correct and will work fine, but the compiler gives you a friendly warning that it can't do compile-time type checking on values entered into and retrieved from the HashMap.
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, i got text to morse done. Now for morse to text. Is there a way i can create a new HashMap that switches the keys and values of the other one? i really don't want to type in all that stuff again.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clay,
You can write a loop to do it. The map.keySet() method will give you a list of all the keys in your original map. When looping through that set, you can get the value (since you have the key) and put them into a new map inverting the key and the value.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic