|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (10 bit)
Join Date: Mar 2006
Location: Maryland
Posts: 550
|
I'm currently writing a java program that converts english text to morse code and vice versa depending on the file. The program accepts a file from user input and writes to a file. The input file may be in english (if it is the text must be all caps) or in morse code (if it is, each character must be on it's own line and an end of a sentence is marked by two line breaks). The way the program checks if the input file is english or morse code is by the first line. The first line has either an "M" for morse code or "E" for english.
So far i've got the encoding correct (english -> morse code). My trouble is the decoding process (morse code -> english). I'm able to read from the file, decode it correctly but the formatting is wrong. Here is an example... Input File (Morse Code): Code:
.- -... -.-. -.. Code:
A B C D Code:
ABCD Here's my source code: http://dl.getdropbox.com/u/244748/Mo...Translate.java
__________________
Kerberos2: Corsair Obsidian 700D ASUS M4A89TD Radeon HD 5850 A-DATA 8GB DDR3 1333 AMD Phenom II x6 1055T @ 2.8GHz w/ Corsair H50 Corsair 850HX PSU WD Blue 160GB 7200 RPM SATA HDD WD Black 640GB 7200 RPM SATA HDD ASUS DVD/CD Drive |
|
|
|
|
|
#2 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
To match the output, you'll have to print a space after each letter.
for the \n, you'll need to print out a \n, and not just a space Also, this would be a good example of where to use the switch-case statement, rather than a chain of if-else
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
|
|
|
|
|
|
#3 |
|
Member (10 bit)
Join Date: Mar 2006
Location: Maryland
Posts: 550
|
Well, this program is for a class im taking and the requirements are as follows...
https://bbweb.towson.edu/courses/1/C...ne_time_token= My professor that we would have to use the "useDelimiter" function. I've read the useDelimiter function in the Java API and I don't understand how to implement it in this. |
|
|
|
|
|
#4 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
Your link doesn't work.
Here's an example function that uses it: http://www.java2s.com/Code/JavaAPI/j...ingpattern.htm And the official API pages (which don't look that helpful): http://java.sun.com/j2se/1.5.0/docs/...va.lang.String) http://java.sun.com/j2se/1.5.0/docs/....regex.Pattern) |
|
|
|
|
|
#5 |
|
Member (10 bit)
Join Date: Mar 2006
Location: Maryland
Posts: 550
|
I've been doing that but I don't know how to use the useDelimiter to create a space between each word when decoding.
My current output is.. THISISATESTand so my professor said to use, "useDelimiter" to fix that issue so it can be.. THIS IS A TESTHere's what I did. Code:
scanIn.useDelimiter("\n");
while(scanIn.hasNextLine())
{
Code:
public static void getDecodeChar(String decodeLine, PrintStream out)
{
if(decodeLine.equals(".-"))
{
out.print("A");
}
else if(decodeLine.equals("-..."))
{
out.print("B");
}
else if(decodeLine.equals("-.-."))
{
out.print("C");
}
else if(decodeLine.equals("-.."))
{
out.print("D");
}
else if(decodeLine.equals("."))
{
out.print("E");
}
else if(decodeLine.equals("..-."))
{
out.print("F");
}
else if(decodeLine.equals("--."))
{
out.print("G");
}
else if(decodeLine.equals("...."))
{
out.print("H");
}
else if(decodeLine.equals(".."))
{
out.print("I");
}
else if(decodeLine.equals(".---"))
{
out.print("J");
}
else if(decodeLine.equals("-.-"))
{
out.print("K");
}
else if(decodeLine.equals(".-.."))
{
out.print("L");
}
else if(decodeLine.equals("--"))
{
out.print("M");
}
else if(decodeLine.equals("-."))
{
out.print("N");
}
else if(decodeLine.equals("---"))
{
out.print("O");
}
else if(decodeLine.equals(".--."))
{
out.print("P");
}
else if(decodeLine.equals("--.-"))
{
out.print("Q");
}
else if(decodeLine.equals(".-."))
{
out.print("R");
}
else if(decodeLine.equals("..."))
{
out.print("S");
}
else if(decodeLine.equals("-"))
{
out.print("T");
}
else if(decodeLine.equals("..-"))
{
out.print("U");
}
else if(decodeLine.equals("...-"))
{
out.print("V");
}
else if(decodeLine.equals(".--"))
{
out.print("W");
}
else if(decodeLine.equals("-..-"))
{
out.print("X");
}
else if(decodeLine.equals("-.--"))
{
out.print("Y");
}
else if(decodeLine.equals("--.."))
{
out.print("Z");
}
else if(decodeLine.equals("\n"))
{
out.print(" ");
}
}
|
|
|
|
|
|
#6 | |
|
Member (10 bit)
|
You're using useDelimiter() correctly, to change the default delimiter from whitespace to a new line.
Unless I'm mistaken, your problem is with formatting the output... so in your getDecodeChar method, whenever you print the output with out.print("?") simply change this to out.print("? ") with a space at the end of the character to print. In your first post, you say the correct output is: Quote:
Craig. |
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|