Results 1 to 11 of 11
  1. #1
    mentoes178 is offline Member
    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

    Default Java Program Morse Code Translator

    I have written this program but unfortunately there is not output... i don't know what I am doing wrong....

    this is for the main:

    Java Code:
     
    import javax.swing.JOptionPane;
    import java.io.*;
    public class Morse
    {
        public static void main(String[] args)throws IOException
        {
            String userInput;
            final String SENTINEL = "0";//for exiting program when entered
            //object creation
            Translate text = new Translate();
            //getting user input to be translated
            do
            {
                userInput = JOptionPane.showInputDialog("Please enter what you wish to translte to Morse code (no punctuation).");
                String compare = userInput.toUpperCase();
                String[] codedText = new String[compare.length()];
                codedText = text.translateHere(compare);
                text.toString(userInput, codedText);
            }while(!userInput.equals(SENTINEL));
        }//end main
    }//end class
    and this for the class:

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Translate {
     public Translate()
        {
        }//end default constructor
        public String[] translateHere(String s)throws IOException
        {
            String compare = s, codedLine = "";  //userInput toUpperCase
            int length = compare.length();  //length of userInput
            String line, file = "Morse.txt";// variable holding file name and variable for each letter/number
            char code;
            //Constants
            final int MAX = 36;
            //Arrays
            char[] morseLetter = new char[MAX];
            String[] morseCode = new String[MAX];
            String[] newMessage = new String[length];
            //putting user input in a character array;
            char[] userLetters = compare.toCharArray();
            //object creation
            File openFile = new File(file);
            Scanner inFile = new Scanner(openFile);
            int  counter = 0;
            while(inFile.hasNext())
                {
                    line = inFile.next();
                    code = (char)line.charAt(0);
                    //System.out.println(code);
                    morseLetter[counter] = code;
                    morseCode[counter] = inFile.next();
                    counter++;
                }//end nested while loop
            for(int j = 0; j < length; j++)
            {
                for(int k = 0; k < MAX; k++)
                {
                    if(userLetters[j] == morseLetter[k])
                    {
                        newMessage[j] = morseCode[k];
                    }
                }//end nested for loop
            }//end for loop
            return newMessage;
        }//end method that completes translateion
        public String toString(String a, String[] b)
    {
       System.out.println("Input: " + a);
       System.out.println("Output:" + );
       String output = "";
       for(int i = 0; i < b.length; i++)
       {
          output = output + b[i];
       }
       return output;
     }//end toString method
    }//end Translate Class

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Program Morse Code Translator

    System.out.println( ... ) is a perfect and cheap debugger; sprinkle in some of them and see if the values of the variables are what you want them to be.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    mentoes178 is offline Member
    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java Program Morse Code Translator

    the output should be the translated input, but for some reason my code isn't printing it

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Program Morse Code Translator

    Quote Originally Posted by mentoes178 View Post
    the output should be the translated input, but for some reason my code isn't printing it
    Yes I know, you wrote that before, that's why I suggested to stick in some debug System.out.println( ... ) statements so you can see what your program is doing.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    mentoes178 is offline Member
    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java Program Morse Code Translator

    that is no help....

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Program Morse Code Translator

    Quote Originally Posted by mentoes178 View Post
    that is no help....
    Opinions differ about it but your subsequent posts contain no additional information.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    mentoes178 is offline Member
    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java Program Morse Code Translator

    how should i get the program to print my return

    Java Code:
    public String toString(String a, String[] b)
    {
       String output = "";
               
       for(int i = 0; i < b.length; i++)
       {
          output = output + b[i];
       }
       return output;
       
     }//end toString method

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Java Program Morse Code Translator

    System.out.println(output);//?

  9. #9
    mentoes178 is offline Member
    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java Program Morse Code Translator

    Quote Originally Posted by SurfMan View Post
    System.out.println(output);//?
    where should that be put in?

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Java Program Morse Code Translator

    The line where the variable "output" would actually contain something. My first guess is just before "return output". :)

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Java Program Morse Code Translator

    One problem with the code is when the toString() method is called, the value that it returns is not used in any way.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Java Program Morse Code Translator
    By mentoes178 in forum New To Java
    Replies: 1
    Last Post: 06-17-2013, 06:11 PM
  2. [HELP] morse code java program
    By g_who in forum New To Java
    Replies: 14
    Last Post: 10-24-2012, 05:15 PM
  3. (help) morse code java => null
    By lazarat in forum New To Java
    Replies: 18
    Last Post: 09-28-2012, 04:39 AM
  4. Morse Code Java Program - Help Needed Please!!
    By dungeondragon in forum New To Java
    Replies: 8
    Last Post: 02-25-2011, 04:36 AM
  5. Morse Code
    By Jamison5213 in forum New To Java
    Replies: 6
    Last Post: 04-14-2010, 04:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •