Results 1 to 7 of 7

Thread: Morse Code

  1. #1
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default Morse Code

    I can't really understand why this isn't working. I'm importing morse code from a text file and putting it into an arraylist and I'm having the user type in letters which will be converted and outputted as morse code. I don't know the issue with these 2 lines though. Can anyone help?

    Java Code:
    /**
     * This programs translates into morse code.
     *
     *
     * Jamison Hyman
     * 4/12/10
     */
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.awt.*;
    import java.io.File;
    import java.io.PrintWriter;
    import java.io.IOException;
    public class MorseCode {
            
      
        public static void main(String[] args) throws IOException
         {
            Scanner in2 = new Scanner(new File("morsecode.txt"));
            ArrayList<String> morse = new ArrayList<String>();
            
            while(in2.hasNext()) {
           		 morse.add(in2.next());
            }
             in2.close();  
           
            
            String [] letters = {"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"}; 
            
            System.out.println("Type a sentence with no punctuation. ");
             Scanner in = new Scanner(System.in);  	
            
                    String user = in.next();
                    ArrayList<String> userLetters = new ArrayList<String>();
                    
              for (int i = 0; i < user.length(); i++)
              	 { 
                      userLetters.add("" + user.charAt(i));
                          }
                          
                for (int i = 0; i <user.length(); i++)
                {[b][u]
                	if (user.charAt(i) = "a")
                	{
                		System.out.print(morse(0));[/u][/b][u][/u]
                	}
                }          
          
    
    			//	System.out.println(userLetters);
    			
    
            
            
             in.close(); 
        }
    }
    The error is "unexpected type : line 43" (if (user.charAt().....))
    and "cannot find symbol method morse(int) : line 45 (System.out.print(morse(0)...)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    char literals require single quotes 'a' not double quotes "a" which denote a String literal. Also, you don't compare a with b using a single = but rather a double ==. The single = is for assignment, not comparison.

    Much luck

  3. #3
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default

    Wow, thanks so much! That worked really well! Here's the new code but I still have one more issue
    Java Code:
                	if (user.charAt(i) == 'a')
                	{
                		System.out.print(morse(0));
                	}
                }
    The line "System.out.print(morse(0));" is giving me the error "cannot find symbol method morse(int)".

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Jamison5213 View Post
    Wow, thanks so much! That worked really well!
    You're welcome.


    Here's the new code but I still have one more issue
    Java Code:
                	if (user.charAt(i) == 'a')
                	{
                		System.out.print(morse(0));
                	}
                }
    The line "System.out.print(morse(0));" is giving me the error "cannot find symbol method morse(int)".
    You appear to be trying to call a method called morse that takes an int parameter and there is no such method. You do however have an ArrayList called morse, and you don't get an element from an array list like that. It's probably in your best interest to learn to use the API to see how to use ArrayList, to try to find which method you should use to get an element from it. The ArrayList API can be found here: ArrayList API. Go through the methods listed there and I'll bet you'll quickly get the one you'll need.

    Much luck!

  5. #5
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default

    Thank you! all done! There are a few errors though but it's good enough for government.

    Java Code:
    /**
     * This programs translates into morse code.
     *
     *
     * Jamison Hyman
     * 4/12/10
     */
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.awt.*;
    import java.io.File;
    import java.io.PrintWriter;
    import java.io.IOException;
    public class MorseCode {
            
      
        public static void main(String[] args) throws IOException
         {
            Scanner in2 = new Scanner(new File("morsecode.txt"));
            ArrayList<String> morse = new ArrayList<String>();
            
            while(in2.hasNext()) {
           		 morse.add(in2.next());
            }
             in2.close();  
           
            
            String [] letters = {"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"}; 
            
            System.out.print("Type a sentence with no punctuation... ");
             Scanner in = new Scanner(System.in);  	
            
                    String user = in.next();
                    ArrayList<String> userLetters = new ArrayList<String>();
                    
              for (int i = 0; i < user.length(); i++)
              	 { 
                      userLetters.add("" + user.charAt(i));
                          }
                          System.out.println();
                          System.out.println();
                          
                for (int i = 0; i <user.length(); i++)
                {
                	if (user.charAt(i) == 'a')
                	{
                		System.out.print(morse.get(0) + ", ");
                	}
                	if (user.charAt(i) == 'b')
                	{
                		System.out.print(morse.get(1) + ", ");
                	}
                	            	if (user.charAt(i) == 'c')
                	{
                		System.out.print(morse.get(2) + ", ");
                	}
                	            	if (user.charAt(i) == 'd')
                	{
                		System.out.print(morse.get(3) + ", ");
                	}
                	            	if (user.charAt(i) == 'e')
                	{
                		System.out.print(morse.get(4) + ", ");
                	}
                	            	if (user.charAt(i) == 'f')
                	{
                		System.out.print(morse.get(5) + ", ");
                	}
                	            	if (user.charAt(i) == 'g')
                	{
                		System.out.print(morse.get(6) + ", ");
                	}
                	            	if (user.charAt(i) == 'h')
                	{
                		System.out.print(morse.get(7) + ", ");
                	}
                	            	if (user.charAt(i) == 'i')
                	{
                		System.out.print(morse.get(8) + ", ");
                	}
                	            	if (user.charAt(i) == 'j')
                	{
                		System.out.print(morse.get(9) + ", ");
                	}
                	            	if (user.charAt(i) == 'k')
                	{
                		System.out.print(morse.get(10) + ", ");
                	}
                	            	if (user.charAt(i) == 'l')
                	{
                		System.out.print(morse.get(11) + ", ");
                	}
                	            	if (user.charAt(i) == 'm')
                	{
                		System.out.print(morse.get(12) + ", ");
                	}
                	            	if (user.charAt(i) == 'n')
                	{
                		System.out.print(morse.get(13) + ", ");
                	}
                	            	if (user.charAt(i) == 'o')
                	{
                		System.out.print(morse.get(14) + ", ");
                	}
                	            	if (user.charAt(i) == 'p')
                	{
                		System.out.print(morse.get(15) + ", ");
                	}
                	            	if (user.charAt(i) == 'q')
                	{
                		System.out.print(morse.get(16) + ", ");
                	}
                	            	if (user.charAt(i) == 'r')
                	{
                		System.out.print(morse.get(17) + ", ");
                	}
                	if (user.charAt(i) == 's')
                	{
                		System.out.print(morse.get(18) + ", ");
                	}
                	if (user.charAt(i) == 't')
                	{
                		System.out.print(morse.get(19) + ", ");
                	}
                	if (user.charAt(i) == 'u')
                	{
                		System.out.print(morse.get(20) + ", ");
                	}
                	if (user.charAt(i) == 'v')
                	{
                		System.out.print(morse.get(21) + ", ");
                	}
                	if (user.charAt(i) == 'w')
                	{
                		System.out.print(morse.get(22) + ", ");
                	}
                	if (user.charAt(i) == 'x')
                	{
                		System.out.print(morse.get(23) + ", ");
                	}
                	if (user.charAt(i) == 'y')
                	{
                		System.out.print(morse.get(24) + ", ");
                	}
                	if (user.charAt(i) == 'z')
                	{
                		System.out.print(morse.get(25) + ", ");
                	}
                	            	if (user.charAt(i) == '1')
                	{
                		System.out.print(morse.get(26) + ", ");
                	}
                	            	if (user.charAt(i) == '2')
                	{
                		System.out.print(morse.get(27) + ", ");
                	}
                	            	if (user.charAt(i) == '3')
                	{
                		System.out.print(morse.get(28) + ", ");
                	}
                	            	if (user.charAt(i) == '4')
                	{
                		System.out.print(morse.get(29) + ", ");
                	}
                	            	if (user.charAt(i) == '5')
                	{
                		System.out.print(morse.get(30) + ", ");
                	}
                	            	if (user.charAt(i) == '6')
                	{
                		System.out.print(morse.get(31) + ", ");
                	}
                	            	if (user.charAt(i) == '7')
                	{
                		System.out.print(morse.get(32) + ", ");
                	}
                	            	if (user.charAt(i) == '8')
                	{
                		System.out.print(morse.get(33) + ", ");
                	}
                	            	if (user.charAt(i) == '9')
                	{
                		System.out.print(morse.get(34) + ", ");
                	}
                	            	if (user.charAt(i) == '0')
                	{
                		System.out.print(morse.get(35) + ", ");
                	}
                					if (user.charAt(i) == ' ')
                					{
                						System.out.print("");
                					}
                }          
          
    
    			
    
            
            
             in.close(); 
        }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Another way to shorten your code, if you know the order of your characters in the array, then use that to your advantage:

    Java Code:
      public static final String CHARACTERS = "abcdefghijklmnopqrstuvwxyz1234567890";
      public static ArrayList<String> morse = new ArrayList<String>();
      
      public static String getMorse(char alphaNumeric) {
        alphaNumeric = Character.toLowerCase(alphaNumeric);
        // get position of input char in array
        int index = CHARACTERS.indexOf(alphaNumeric);
        if (index < 0) {
          throw new IllegalArgumentException("bad alpha numeric parameter");
        }
        else {
          // use that index to return the corresponding morse ArrayList item
          return morse.get(index);
        }
      }

Similar Threads

  1. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  2. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •