6 Replies - 7561 Views - Last Post: 16 June 2011 - 05:25 PM Rate Topic: -----

#1 HelloBoston  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 06-June 11

Java Morse Code Converter

Posted 16 June 2011 - 12:42 PM

Hey guys, back with another quick question.

I'm looking to make a Morse code converter that does the following:
1. Prompts user to select a translation from Morse to letters, or from letters to Morse*
2. Prompts user to enter what they want translated
3. Display the translation.

So far in my program (below) I have the code to ask a user for a sentence and translate that sentence into Morse code. This works perfectly. I'm wondering, in simplest terms, how to go about prompting the user if they want to translate from Morse to letters and from letters to Morse.*

package jvfinalproject1;
import java.io.*;
/**
 *
 * @author George Teng
 */
public class Main {

    public static void main(String[] args) {
        BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));


   String[] morse = {".- ","-... ","-.-/>. ","-.. ",". ","..-. ","--. ",".... ",".. ",
   ".--- ","-.-/> ",".-.. ","-- ","-. ","--- ",".--. ","--.-/> ",".-. ","... ","- ","..- ",
   "...- ",".-- ","-..- ","-.-/>- ","--.. "};




BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

String sentence = "";
System.out.println("Enter a sentence");
try{
sentence = dataIn.readLine();
}catch(Exception e){
System.out.println("Invalid Input");
}

for(int x = 0; x<sentence.length(); x++){

char ch = sentence.charAt(x);





switch(ch){
case 'a':
case 'A':
System.out.println(morse[0]); break;

case 'b':
case 'B':
System.out.print(morse[1]); break;

case 'c':
case 'C':
System.out.print(morse[2]); break;

case 'd':
case 'D':
System.out.print(morse[3]); break;

case 'e':
case 'E':
System.out.print(morse[4]); break;

case 'f':
case 'F':
System.out.print(morse[5]); break;

case 'g':
case 'G':
System.out.print(morse[6]); break;

case 'h':
case 'H':
System.out.print(morse[7]); break;

case 'i':
case 'I':
System.out.print(morse[8]); break;

case 'j':
case 'J':
System.out.print(morse[9]); break;

case 'k':
case 'K':
System.out.print(morse[10]); break;

case 'l':
case 'L':
System.out.print(morse[11]); break;

case 'm':
case 'M':
System.out.print(morse[12]); break;

case 'n':
case 'N':
System.out.print(morse[13]); break;

case 'o':
case 'O':
System.out.print(morse[14]); break;

case 'p':
case 'P':
System.out.print(morse[15]); break;

case 'q':
case 'Q':
System.out.print(morse[16]); break;

case 'r':
case 'R':
System.out.print(morse[17]); break;

case 's':
case 'S':
System.out.print(morse[18]); break;

case 't':
case 'T':
System.out.print(morse[19]); break;

case 'u':
case 'U':
System.out.print(morse[20]); break;

case 'v':
case 'V':
System.out.print(morse[21]); break;

case 'w':
case 'W':
System.out.print(morse[22]); break;

case 'x':
case 'X':
System.out.print(morse[23]); break;

case 'y':
case 'Y':
System.out.print(morse[24]); break;

case 'z':
case 'Z':
System.out.print(morse[25]); break;
}
}
System.out.println(".");

}

}
    



Is This A Good Question/Topic? 0
  • +

Replies To: Java Morse Code Converter

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10825
  • View blog
  • Posts: 40,363
  • Joined: 27-December 08

Re: Java Morse Code Converter

Posted 16 June 2011 - 12:44 PM

Prompt and ask them, and manage a boolean representing the translation: morse to letters or vice versa. Also, convert the String toUpperCase(), then just subtract 'A' from the charAt(i) to get the conversion.

System.out.println(morse[someLetter - 'A']);


Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,001
  • Joined: 20-September 08

Re: Java Morse Code Converter

Posted 16 June 2011 - 01:45 PM

You'd probably be better off using a Map or your two methodologies (translation each way) will be disjunct
Was This Post Helpful? 1
  • +
  • -

#4 HelloBoston  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 06-June 11

Re: Java Morse Code Converter

Posted 16 June 2011 - 02:16 PM

Alright, I've written what I thought was equivalent code to go from Morse to Text. I understand this is NOT the most efficient way to go about doing this, but my Java instructions say to do so.
public class MorseCodeTranslator
{
  public static String CovertToChar(char morsecode)
  {
	char letter;
	morsecode = Character.toLowerCase(morsecode);
	String output = "";
	
	if(morsecode.charAt(0) == '-')
	{
	  if(morsecode.length() == 1)
		letter = 'T';
	}
	else
	  if(morsecode.charAt(1) == '-')
	{
	  if(morsecode.length() == 2)
		letter = 'M';
	}
	else
	  if(morsecode.charAt(2) == '-')
	{
	  if(morsecode.length() == 3)
		letter = 'O';
	}
	else
	  if(morsecode.charAt(0) == '-.')
	{
	  if(morsecode.length() == 1)
		letter = 'N';
	}
	else
	  if(morsecode.charAt(0) == '-..')
	{
	  if(morsecode.length() == 1)
		letter = 'D';
	}
	else
	  if(morsecode.charAt(0) == '-.-/>')
	{
	  if(morsecode.length() == 1)
		letter = 'K';
	}
	else
	  if(morsecode.charAt(0) == '--.')
	{
	  if(morsecode.length() == 1)
		letter = 'G';
	}
	else
	  if(morsecode.charAt(0) == '--..')
	{
	  if(morsecode.length() == 1)
		letter = 'Z';
	}
	else
	  if(morsecode.charAt(0) == '--.-/>')
	{
	  if(morsecode.length() == 1)
		char = 'Q';
	}
	else
	  if(morsecode.charAt(0) == '-.-/>-');
	{
	  if(morsecode.length() == 1)
		letter = 'Y';
	}
	else
	  if(morsecode.charAt(0) == '-.-/>.');
	{
	  if(morsecode.length() == 1)
		letter = 'C';
	}
	else
	  if(morsecode.charAt(0) == '-..-');
	{
	  if(morsecode.length() == 1)
		letter = 'X';
	}
	else
	  if(morsecode.charAt(0) == '-...');
	{
	  if(morsecode.length() == 1)
		letter = 'B';
	}

	//start with all combinations starting with dit(dot)
	if (morsecode.charAt(0) == '.')
	{
	  if(morsecode.length() == 1)
		letter = 'E';
	}
	else
	  if(morsecode.charAt(1) == '.')
	{
	  if(morsecode.length() == 2)
		letter = 'I';
	}
	else
	  if(morsecode.charAt(2) == '.')
	{
	  if(morsecode.length() == 3)
		letter = 'S';
	}
	else
	  if(morsecode.charAt(3) == '.')
	{
	  if(morsecode.length() == 4)
		letter = 'H';
	}
	else
	  if(morsecode.charAt(0) == '.-')
	{
	  if(morsecode.length() == 1)
		letter = 'A';
	}
	else
	  if(morsecode.charAt(0) == '.--')
	{
	  if(morsecode.length() == 1)
		letter = 'W';
	}
	else
	  if(morsecode.charAt(0) == '.-.')
	{
	  if(morsecode.length() == 1)
		letter = 'R';
	}
	else
	  if(morsecode.charAt(0) == '..-')
	{
	  if(morsecode.length() == 1)
		letter = 'U';
	}
	else
	  if(morsecode.charAt(0) == '.---')
	{
	  if(morsecode.length() == 1)
		letter = 'J';
	}
	else
	  if(morsecode.charAt(0) == '.--.')
	{
	  if(morsecode.length() == 1)
		letter = 'P';
	}
	else
	  if(morsecode.charAt(0) == '.-..')
	{
	  if(morsecode.length() == 1)
		letter = 'L';
	}
	else
	  if(morsecode.charAt(0) == '..-.')
	{
	  if(morsecode.length() == 1)
		letter = 'F';
	}
	else
	  if(morsecode.charAt(0) == '...-')
	{
	  if(morsecode.length() == 1)
		letter = 'V';
	}
  }
  return ouput;
}



However, I am getting various errors with each line of code, reading:
"<identifier> expected

illegal start of type

unclosed character literal

malformed floating point literal

unclosed character literal"
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,001
  • Joined: 20-September 08

Re: Java Morse Code Converter

Posted 16 June 2011 - 02:30 PM

Your code doesn't make sense - why would you have a method called 'ConvertToChar' with a char as input?
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java Morse Code Converter

Posted 16 June 2011 - 03:56 PM

And just a couple more things on top of the other comments. The method returns the variable output but you never assign anything to it. Therefore it will always return an empty String. And I havnt looked much into the Char class, but shouldnt the charAt method be looking at an individual character, but places like this (charAt(0) == '--..') your char holds 4 characters (someone needs to confirm if this is correct or not).
Was This Post Helpful? 0
  • +
  • -

#7 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: Java Morse Code Converter

Posted 16 June 2011 - 05:25 PM

View Postg00se, on 16 June 2011 - 03:30 PM, said:

You'd probably be better off using a Map or your two methodologies (translation each way) will be disjunct


I agree with g00se. I've written a morse code converter before, and I used a Hash Map to contain the two character sets to translate between. Iterate over the inputted String and look up each encountered character in the Hash Map. If you find a match, output the appropriate value.

This post has been edited by Codebug: 17 June 2011 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1