Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 11-15-2008, 03:29 PM   #1
Member (10 bit)
 
faint545's Avatar
 
Join Date: Mar 2006
Location: Maryland
Posts: 550
Exclamation Java; useDelimiter

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:
.-
-...
-.-.

-..
CORRECT Output File (English):
Code:
A B C
D
BUT I'm getting...

Code:
ABCD
I know i'm supposed to use the Scanner function, useDelimiter, but i'm not sure how to implement it.

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
faint545 is offline   Reply With Quote
Old 11-15-2008, 04:01 PM   #2
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
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.
Force Flow is offline   Reply With Quote
Old 11-17-2008, 08:08 PM   #3
Member (10 bit)
 
faint545's Avatar
 
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.
faint545 is offline   Reply With Quote
Old 11-18-2008, 07:59 AM   #4
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
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)
Force Flow is offline   Reply With Quote
Old 11-18-2008, 11:51 AM   #5
Member (10 bit)
 
faint545's Avatar
 
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..
THISISATEST
and so my professor said to use, "useDelimiter" to fix that issue so it can be..
THIS IS A TEST
Here's what I did.

Code:
scanIn.useDelimiter("\n");
while(scanIn.hasNextLine())
{
String getLine = scanIn.nextLine(); getDecodeChar(getLine, write);
}
and here's the function getDecodeChar()

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(" ");
        }
    }
Link: http://dl.getdropbox.com/u/244748/Assignment%203.pdf
faint545 is offline   Reply With Quote
Old 11-26-2008, 06:22 AM   #6
Member (10 bit)
 
Craig100's Avatar
 
Join Date: Sep 2003
Location: Edinburgh
Posts: 572
Send a message via MSN to Craig100
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:
A B C
D
Is this a typo with the D on a new line? or do you need it to print blocks of 3 per line?

Craig.
Craig100 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:39 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2