Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Help with Morse Code program please.

    So for this assignment I have to input a statement and translate it into Morse code.

    Here is my class Translate

    public class Translate
    {
    String input;
    String morseCode[];
    char characters[];

    public Translate(String input)
    {
    this.input = input;
    }

    public void setInput(String input)
    {
    this.input = input;
    }

    public void setCharacters(char[] characters)
    {
    this.characters = characters;
    }

    public void setMorseCode(String[] morseCode)
    {
    this.morseCode = morseCode;
    }

    public String getInput()
    {
    return input;
    }

    public String getTranslate()
    {
    String output = "";
    for(int i=0; i<input.length(); i++)
    {
    for(int index=0; index<characters.length; index++)
    {
    if(input.charAt(i) == characters[index])
    {
    output = output + " " + morseCode[index];
    }
    }
    }
    return output;
    }
    }

    And here is my main class Morse

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

    //Declaration of class name
    public class Morse
    {
    public static void main(String[]args)throws IOException
    {

    //Declaration of all arrays and instance fields
    String line, name, file = "Code.txt";

    String[] numLetters;
    numLetters = new String[40];

    String[] morseCode;
    morseCode = new String[40];

    int i = 0;

    int index = 0;

    File openFile = new File (file);
    Scanner inFile = new Scanner (openFile);

    char[] characters;

    while(inFile.hasNext())
    {

    numLetters[i] = inFile.next();

    line = numLetters[i];

    characters = line.toCharArray();

    morseCode[i] = inFile.next();

    i=i+1;

    }


    Scanner keyboard = new Scanner(System.in);

    char[] inputArray;

    String userInput;
    String input;

    System.out.println("Enter something to translate: ");
    userInput= keyboard.nextLine();

    input = userInput.toUpperCase();
    inputArray = input.toCharArray();

    for(int j=0; j<inputArray.length; j++)
    {
    if(Character.isLetter(inputArray[j]) || Character.isDigit(inputArray[j]))
    {
    System.out.println(inputArray[j]);
    }
    }

    Translate inputTranslate = new Translate(input);

    inputTranslate.setInput(input);

    inputTranslate.setCharacters(inputArray);

    inputTranslate.setMorseCode(morseCode);

    System.out.println(inputTranslate.getTranslate());

    inFile.close();
    }
    }

    We had to take a text file and store numbers and the alphabet into one array and then store the Morse equivalent
    into another parallel array.

    My program compiles but it isn't outputting the correct Morse code for the characters inputted.

    Can anybody help me with this?

  2. #2
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Help with Morse Code program please.

    Anybody?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help with Morse Code program please.

    Quote Originally Posted by Steffer_Neffer_15 View Post
    My program compiles but it isn't outputting the correct Morse code for the characters inputted.
    What is your program printing? Are you sure your program read all the data correctly? You can print the intermediate results so you can trace what your program is doing ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Help with Morse Code program please.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    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: 10
    Last Post: 06-18-2013, 12:03 AM
  2. Java Program Morse Code Translator
    By mentoes178 in forum New To Java
    Replies: 1
    Last Post: 06-17-2013, 06:11 PM
  3. [HELP] morse code java program
    By g_who in forum New To Java
    Replies: 14
    Last Post: 10-24-2012, 05:15 PM
  4. Morse Code Help Please!!
    By javaa in forum New To Java
    Replies: 25
    Last Post: 09-28-2012, 03:28 AM
  5. Morse Code Java Program - Help Needed Please!!
    By dungeondragon in forum New To Java
    Replies: 8
    Last Post: 02-25-2011, 04:36 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
  •