Results 1 to 9 of 9
  1. #1
    dungeondragon is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Morse Code Java Program - Help Needed Please!!

    So here's what the program has to do. We have to make a text file (morsecode.txt) that contains all the morse code characters for A-Z. So it would look something like this:

    .-
    -...
    -.-.
    -..

    Which are A, B, C, and D, all the way up to Z. The program asks the user to enter a sentence, such as, "My name is dungeondragon," and the program has to read the morse code letters from the text file, and translate the english that the user enters to morse code. So far, I'm really stumped. I've only gotten as far as reading the text file in, and assigning it to an ArrayList.



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

    public class MorseCode
    {
    public static void readFile() throws IOException
    {
    Scanner inFile = new Scanner(new File("morsecode.txt"));

    ArrayList translations = new ArrayList();
    translations.add(inFile);
    }
    }


    Any help on where I can go next? All methods much be static, by the way. Thanks!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Are you allowed to use a Map. It will be much simpler to put each letter and the corresponding morse code into a Map. Then for each letter (convert to uppercase or lowercase to match your Map) look up the morse code in the Map.

  3. #3
    dungeondragon is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    We haven't learned about Maps yet, but if you can just point in the right direction using something different I would greatly appreciate it. I'm just honestly kinda lost at this point.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java API and Java tutorials have all the information you need.

    But I would consult your teacher first. If you cannot use a Map then how about writing your own class that holds the Alphabet character and the Morse code. Then create instances of this class and store in the ArrayList.

  5. #5
    dungeondragon is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    My teacher has provided me with this code, in which she claims should help, but I don't understand what she's done with it:

    for (int i = 0; i<myCode.length(); i++)
    {
    String myChar = myCode.substring(i,i+1);
    if (myChar.equals(" "))
    {
    output+=" / ";
    continue;
    }
    String myMorse = translations.get(alphabet.indexOf(myChar));
    output+=myMorse;
    if (i != myCode.length()-1)
    output+=" ";
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    for (int i = 0; i<myCode.length(); i++) { // use a loop to iterate over user input stored in myCode
        String myChar = myCode.substring(i,i+1); // extract a single character from user input. Could have used charAt instead
        if (myChar.equals(" ")) {
            output+=" / "; // if current character is a space (ie end of a word) add a slash to output to separate words in morse code
            continue; // go to next loop iteration
        }
        String myMorse = translations.get(alphabet.indexOf(myChar)); // alphabet would be a String with each letter in it "ABCDE.....". Get the index of where current character is ie A=0, B=1, etc. Then get the Morse code at the same index ie .- will be at index 0 to match A
        output+=myMorse; // add that morse code to output
        if (i != myCode.length()-1)
            output+=" "; // if we are anywhere except at the end of user input add a space to output.
    }

  7. #7
    dungeondragon is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Thank you very much junky!

    OK, now my only problem, the problem that's been preventing me from finishing this program all along, is where " String myMorse = translations.get(alphabet.indexOf(myChar)); // alphabet would be a String with each letter in it "ABCDE.....". Get the index of where current character is ie A=0, B=1, etc. Then get the Morse code at the same index ie .- will be at index 0 to match A"

    I know how to read my morsecode.txt into the java program, but I don't know how o get it to realize that the first line corresponds to A, the second line corresponds to B, and so forth.

  8. #8
    dungeondragon is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Also, when I try creating a String alphabet = "ABCDE.." I get this error:

    found : java.lang.Object
    required: java.lang.String

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    import java.util.ArrayList;
    
    class Fubar {
        ArrayList<String> foo = new ArrayList<String>();
        String alphabet = "ABC";
        
        Fubar() {
           foo.add("This is letter A");
            foo.add("This is letter B");
            foo.add("This is letter C");
        }
    
        public void doStuff() {
            print("BCA");
            print("CAB");
            print("ACBA");
        }
    
        private void print(String s) {
            for(int index = 0; index < s.length(); index++) {
                System.out.println(foo.get(alphabet.indexOf(s.charAt(index))));
            }
            System.out.println();
        }
        
        public static void main(String[] args) {
            Fubar f = new Fubar();
            f.doStuff();
        }
    }
    Example code that basically does what you want.

Similar Threads

  1. My First Java Program Code Achievment^^
    By hitech83 in forum New To Java
    Replies: 0
    Last Post: 01-24-2011, 11:01 PM
  2. Morse Code
    By Jamison5213 in forum New To Java
    Replies: 6
    Last Post: 04-14-2010, 04:56 AM
  3. Help needed very simple java program
    By pavez in forum New To Java
    Replies: 3
    Last Post: 08-22-2009, 03:05 PM
  4. Help needed with java arrays code
    By d24706 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:11 AM
  5. code for making a java swing program a demo verson
    By fakhruddin in forum AWT / Swing
    Replies: 1
    Last Post: 11-27-2007, 08:54 PM

Posting Permissions

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