Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Morse Code

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Morse Code

    hi I am writing a program that translates a string to morse code, but my output is a little strange. can anyone push me in the right direction? heres my code

     
    public class Translate
    {
       String sentence;
     
       public Translate(String sentence)
       {
          this.sentence = sentence;
       }
     
       public String getSentence()
       {
             for(int i = 0; i < sentence.length(); i++)
    		      {
    		      	switch(sentence.charAt(i))
                   {
                      case 'a': System.out.print(".-");
                      break;
                      case 'b': System.out.print( "- "); 
                      break; 
                      case 'c': System.out.print( "-.-. "); 
                      break; 
                      case 'd': System.out.print( "-.. "); 
                      break; 
                      case 'e': System.out.print( ". "); 
                      break; 
                      case 'f': System.out.print( "..-. "); 
                      break; 
                      case 'g': System.out.print( "--. "); 
                      break; 
                      case 'h': System.out.print( ". "); 
                      break; 
                      case 'i': System.out.print( ".. "); 
                      break; 
                      case 'j': System.out.print( ".--- "); 
                      break; 
                      case 'k': System.out.print( "-.- "); 
                      break; 
                      case 'l': System.out.print( ".-.. "); 
                      break; 
                      case 'm': System.out.print( "-- "); 
                      break; 
                      case 'n': System.out.print( "-. "); 
                      break; 
                      case 'o': System.out.print( "--- "); 
                      break; 
                      case 'p': System.out.print( ".--. "); 
                      break; 
                      case 'q': System.out.print( "--.- "); 
                      break; 
                      case 'r': System.out.print( ".-. "); 
                      break; 
                      case 's': System.out.print( "... "); 
                      break; 
                      case 't': System.out.print( "- "); 
                      break; 
                      case 'u': System.out.print( "..- "); 
                      break; 
                      case 'v': System.out.print( "...- "); 
                      break; 
                      case 'w': System.out.print( ".-- "); 
                      break; 
                      case 'x': System.out.print( "-..- "); 
                      break; 
                      case 'y': System.out.print( "-.-- "); 
                      break; 
                      case 'z': System.out.print( "--.. "); 
                      break; 
                      case ' ': System.out.print( " | "); 
                      break;
                      case '0': System.out.print("-----");
                      break;
                      case '1': System.out.print(".----");
                      break;
                      case '2': System.out.print("..---");
                      break;
                      case '3': System.out.print("...--");
                      break;
                      case '4': System.out.print("....-");
                      break;
                      case '5': System.out.print(".....");
                      break;
                      case '6': System.out.print("-....");
                      break;
                      case '7': System.out.print("--...");
                      break;
                      case '8': System.out.print("---..");
                      break;
                      case '9': System.out.print("----.");
                      break;
                      case '.': System.out.print(".-.-");
                      break;
                      case ',': System.out.print("--..--");
                      break;
                      case '?': System.out.print("..--..");
                      break;
                }
     
          }
           return sentence;
       }
    }

     
    //This program creates a list of songs for a CD by reading from a file
    import java.io.*;
    import java.util.Scanner;
     
    public class Morse
    {
       public static void main(String [] args) throws IOException
       {
          File file = new File("Morse.txt");
          Scanner input = new Scanner(file);
     
     
     
          char[] characters = new char[36];
          String[] symbols = new String[36];
     
     
     
     
     
          for(int i = 0; i < characters.length; i++) 
          { 
             String line = input.nextLine();
     
             characters[i] = line.charAt(0);
             symbols[i] = line.substring(1);
     
     
          }
     
     
          Scanner keyboard = new Scanner(System.in);
          String sentence;
     
          System.out.println("Enter the sentence that you wish to be decoded: ");
     
          sentence = keyboard.nextLine();
          Translate morse = new Translate(sentence);
     
     
          char[] c = new char[sentence.length()]; 
     
          for(int j = 0; j < sentence.length(); j++)
          {
     
              morse.getSentence();
              c[j] = sentence.charAt(j);
              System.out.println("This translates to : " + c);
          }
     
     
       }
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: Morse Code

    What is strange? Do you get Greek characters? Help us help you by posting what you get and what you expect.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Morse Code

    The scanner is not visible in the for loop because of variable scope.

    Try making the scanner static (belonging to the class).

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: Morse Code

    Quote Originally Posted by Starstreak View Post
    The scanner is not visible in the for loop because of variable scope.
    Wrong!
    The OP didn't state what his actual problem is. He's wildly mixing Strings and chars, file and input all over the place.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Morse Code

    Quote Originally Posted by PhHein View Post
    Wrong!
    The OP didn't state what his actual problem is. He's wildly mixing Strings and chars, file and input all over the place.
    I'm addressing the first error. I believe the diagnosis is correct (scope).

    There are other errors, but we usually address them starting with the first.

    Feel free to test the code yourself, which you clearly have not.

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: Morse Code

    There's no need to test. No Scanner instance is out of scope. Both are defined in the main method outside of other code blocks. That's why your answer is wrong.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Morse Code

    this is a sample output

    Enter the sentence that you wish to be decoded:
    hello
    ..... .-.. .-.. --- This translates to : [C@15e83f9
    ..... .-.. .-.. --- This translates to : [C@15e83f9
    ..... .-.. .-.. --- This translates to : [C@15e83f9
    ..... .-.. .-.. --- This translates to : [C@15e83f9
    ..... .-.. .-.. --- This translates to : [C@15e83f9

    --- Update ---

    the morse code translation is in front of the statement I asked to be printed and it prints as many times as there are letters in the string

    --- Update ---

    I believe it does this because the print statement is in the loop correct?

  8. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: Morse Code

    You seem to use other code than that you've posted. What happened to the file reading, which is more than buggy?

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Morse Code

    that's all the code.... well except for the morse.txt file that i read into the arrays

    --- Update ---

    1 .----
    2 ..---
    3 ...--
    4 ....-
    5 .....
    6 -....
    7 --...
    8 ---..
    9 ----.
    0 -----
    A .-
    B -...
    C -.-.
    D -..
    E .
    F ..-.
    G --.
    H ....
    I ..
    J .---
    K -.-
    L .-..
    M --
    N -.
    O ---
    P .--.
    Q --.-
    R .-.
    S ...
    T -
    U ..-
    V ...-
    W .--
    X -..-
    Y -.--
    Z --..

  10. #10
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: Morse Code

    You still haven't posted, what the expected output is. And why do you read the file without ever using anything from it?

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Morse Code

    sorry. the expected output is the original string and its morse code equivilant

  12. #12
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: Morse Code

    The original is sentence and the morse is morse.getSentence() .I have no idea what that for loop is good for and why you need char[] c.

  13. The Following User Says Thank You to PhHein For This Useful Post:

    m49er704 (April 24th, 2013)

  14. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Morse Code

    i was thinking i had to pass the sentence through some kind of loop so i could evaluate each letter by itself

  15. #14
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: Morse Code

    No, the loop is implemented in in that Translate class. I'd change the logic though. In your Translate class you could read the file in a static initializer block and provide a static method :
    public static String getMorse(String plaintext)
    . From your Morse class you just call
    String morseEncoded = Translate.getMorse(input);

    You should also convert the input to lower case as the output for Hello and hello won't be the same.

  16. The Following User Says Thank You to PhHein For This Useful Post:

    m49er704 (April 24th, 2013)

  17. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Morse Code

    ok thanks for the info. ill come back with what i have changed later

Similar Threads

  1. Replies: 4
    Last Post: January 24th, 2013, 10:20 AM
  2. Replies: 5
    Last Post: November 14th, 2012, 09:47 AM
  3. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  4. morse code
    By daz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 8th, 2012, 05:50 AM
  5. [SOLVED] Translate sentence to morse cod?
    By CSUTD in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 27th, 2012, 02:38 PM