7 Replies - 611 Views - Last Post: 10 October 2012 - 03:50 AM Rate Topic: -----

#1 g_who  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

[QUESTION] morse code to english java program

Posted 08 October 2012 - 04:51 AM

Hi there . I'm just wondering . I want to create a program which is translate a text file with morse code phrases and that phrases move to another text file but now it is in english .


I've done several code that comes in my mind . But i think there's something wrong .

Anyway . I'm new from this site . I search through internet and i saw this . I'm still in first year college from programming course and i want to learn in advance . If you don't mind by the way !

Please don't criticize me .

Here's what I've done .

import java.util.Scanner;
import java.io.*;

public class MorseCodes{
	public static void main(String args[]) throws Exception{		
		File codeFile = new File("morsecode.txt");
		File resultFile = new File("result.txt");
		Scanner codeScan = new Scanner(codeFile);
		PrintWriter resultWriter = new PrintWriter(resultFile);
		
		String givenWord = " ";
		
		while(codeScan.hasNext()){
			switch(givenWord = codeScan.nextLine()){
				case ".-":
					resultWriter.println('A');
					break;
				case "-...":
					resultWriter.println('B');
						break;
				case "-.-/>.":
					resultWriter.println('C');
					break;
				case "-..":
					resultWriter.println('D');
					break;
				case ".":
					resultWriter.println('E');
					break;
				case "..-.":
					resultWriter.println('F');
					break;
				case "--.":
					resultWriter.println('G');
					break;
				case "....":
					resultWriter.println('H');
					break;
				case "..":
					resultWriter.println('I');
					break;
				case ".---":
					resultWriter.println('J');
					break;
				case "-.-/>":
					resultWriter.println('K');
					break;
				case ".-..":
					resultWriter.println('L');
					break;
				case "--":
					resultWriter.println('M');
					break;
				case "-.":
					resultWriter.println('N');
					break;
				case "---":
					resultWriter.println('O');
					break;
				case ".--.":
					resultWriter.println('P');
					break;
				case "--.-/>":
					resultWriter.println('Q');
					break;
				case ".-.":
					resultWriter.println('R');
					break;
				case "...":
					resultWriter.println('S');
					break;
				case "-":
					resultWriter.println('T');
					break;
				case "..-":
					resultWriter.println('U');
					break;
				case "...-":
					resultWriter.println('V');
					break;
				case ".--":
					resultWriter.println('W');
					break;
				case "-..-":
					resultWriter.println('X');
					break;
				case "-.-/>-":
					resultWriter.println('Y');
					break;
				case "--..":
					resultWriter.println('Z');
					break;
				case ".----":
					resultWriter.println('1');
					break;
				case "..---":
					resultWriter.println('2');
					break;
				case "...--":
					resultWriter.println('3');
					break;
				case "....-":
					resultWriter.println('4');
					break;
				case ".....":
					resultWriter.println('5');
					break;
				case "-....":
					resultWriter.println('6');
					break;
				case "--...":
					resultWriter.println('7');
					break;
				case "---..":
					resultWriter.println('8');
					break;
				case "----.":
					resultWriter.println('9');
					break;
				case "-----":
					resultWriter.println('0');
					break;
				default:
					resultWriter.println("Invalid Input");
					break;
			}
			
		}
		
		resultWriter.close();
	}
}




I know from my self . I did a lot of mistake here .
But that's the first thing come's on my mind .
I use switch method to read a morse code one by one that is equal to its corresponding letter .
but i know there's something is missing . hope someone will help me .

Thank you .

Is This A Good Question/Topic? 0
  • +

Replies To: [QUESTION] morse code to english java program

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: [QUESTION] morse code to english java program

Posted 08 October 2012 - 05:19 AM

Quote

while(codeScan.hasNext()){


Well that should be hasNextLine
Was This Post Helpful? 0
  • +
  • -

#3 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: [QUESTION] morse code to english java program

Posted 08 October 2012 - 08:43 AM

The code you have written assumes that you have a single Morse code per line.. for example

.-
--..



will print out

A
Z



If you would like to support multiple codes per line, you will need to switch things around a little bit. One way to do this is to read a line at a time from the input file, convert every Morse code to a letter and then spit out the whole line to resultWriter.

I am not a Morse expert, but you might want to figure out word breaks/spaces as well from your input file.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [QUESTION] morse code to english java program

Posted 08 October 2012 - 03:14 PM

Using a switch() is an horrible solution
Use a HashMap or if you are not allowed to a simple array
Your code should shrink by 80%
Was This Post Helpful? 0
  • +
  • -

#5 g_who  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

Re: [QUESTION] morse code to english java program

Posted 09 October 2012 - 01:29 AM

Thanks for the reply . Hope it will help me .
I will try it anyway .
But one more thing ! I don't even know what is hashmap ?
Was This Post Helpful? 0
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: [QUESTION] morse code to english java program

Posted 09 October 2012 - 02:00 AM

You could probably use 2 arrays for this say,

string[] dotDash= {".-", "-...",...,"-.."};
string[] alphabet={"a","b",..."z"};       



and a function containing a loop which iterates the dots and dashes to form a word.

public String ConvertIntoAlphabet(String inputData ) 
        {
	   for(int i = 0; i < dotDash.length; i++) 
              {
	       if(inputData.equals(dotDash[i]))
	          return alphabet[i];
	      } 
         return;
        }



regards,
Raghav

This post has been edited by raghav.naganathan: 09 October 2012 - 02:02 AM

Was This Post Helpful? 0
  • +
  • -

#7 g_who  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

Re: [QUESTION] morse code to english java program

Posted 09 October 2012 - 11:39 PM

thanks raghav.naganathan .
i will do it .
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [QUESTION] morse code to english java program

Posted 10 October 2012 - 03:50 AM

Use a HashMap<String><Character>

http://docs.oracle.c...il/HashMap.html

String[] morse = {".-", "-...",...,"-.."};  
String[] alpha =  "abcdefghi...xyz"; 

....
    HashMap<String><Character> hm = new HashMap<String><Character>(alpha.length());
    for(int i = 0; i < morse.length; ++i
      hm.put(morse[i], alpha.charAt(i));

....
    char letter = hm.get("-.-/>.");


letter should contain 'c'
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1