4 Replies - 5872 Views - Last Post: 31 March 2009 - 11:44 PM Rate Topic: -----

#1 awcosc14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 08-March 09

convert character to morse code

Posted 30 March 2009 - 04:38 PM

mport java.util.Scanner;
public class CovertToMorse
{
  public class void main(String[] args)
  {
	//declare variables
	String morsecode;
	Scanner input = new Scanner(System.in);
	
	//start with all combinations starting with dah(dash)
	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)
		letter = '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'L';
	}
	else
	  if(morsecode.charAt(0) == '..-.')
	{
	  if(morsecode.length() == 1)
		letter = 'F';
	}
	else
	  if(morsecode.charAt(0) == '...-')
	{
	  if(morsecode.length() == 1)
		letter = 'V';
	}
	}
}



Quote

This program is supposed to read a given character from the user and then displays it in morse code. I need to include a function to do the conversion. I have written out all the possibilities, but I'm not sure how to define the function to do the conversion.


Is This A Good Question/Topic? 0
  • +

Replies To: convert character to morse code

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: convert character to morse code

Posted 30 March 2009 - 04:55 PM

Are you allowed to use a hashtable? That would be the easiest way to do what your problem description says. The code you've posted is going the other direction: you are translating dot-dashes to characters. not characters to dot-dashes.
Was This Post Helpful? 0
  • +
  • -

#3 awcosc14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 08-March 09

Re: convert character to morse code

Posted 30 March 2009 - 05:12 PM

View Postn8wxs, on 30 Mar, 2009 - 03:55 PM, said:

Are you allowed to use a hashtable? That would be the easiest way to do what your problem description says. The code you've posted is going the other direction: you are translating dot-dashes to characters. not characters to dot-dashes.


Quote

We have not learned what that is. I'm sorry that's what I meant to say, the code it translating dot-dashes to characters. The code within the function should use nested if and if-else statements.

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: convert character to morse code

Posted 30 March 2009 - 05:13 PM

Or at least make an array:

{"-.", "-...", "-.-.",

Anyhow we have a Morse specialist here at DIc: n8wxs
I'll let him answer

Edited: oups... double post... (I knew He would be here :-))

This post has been edited by pbl: 30 March 2009 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: convert character to morse code

Posted 31 March 2009 - 11:44 PM

I would use the fact that the Morse code uses variably length coding:
1 - letters E, T
2 - letters A, I, M, N
3 - letters D, G, K, 0, R, S, U, W
4 - letters B, C, F, H, J, L, P, Q, V, X, Y, Z
5 - numbers 1 to 0,
6 - punctuation . , ?

...
String morsecode;

Scanner input = new Scanner(System.in);

while (input.hasNext()) {
   morsecode = input.next();

   if (morsecode.length() == 1) {
	  if (morsecode.charAt(0) == '-')
		 letter = 'T';
	  else
		 letter = 'E';
	  }
   }
   else if (morsecode.length() == 2) {
	  if (morsecode == ".-")
		 letter = 'A';
	  if (morsecode == "..")
		 letter = 'I';
	  ...
   }
   ...
   System.out.print(letter);
}
...


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1