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 24 of 24

Thread: Morse Problem

  1. #1
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Morse Problem

    /**
     * Write a description of class MorseCode here.
     * 
     * @author (Landon L) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class MorseCode 
    {
        private static String[] morse; 
        private static String phrase;
        /**
         * Constructor for objects of class MorseCode
         */
        public MorseCode(String p)
        {
            morse = new String[36];
            phrase = p;
        }
     
        public static String[] read() throws IOException
        {
            Scanner in = new Scanner(new File("morsecode.txt"));
            String a = phrase;
            int i = 0;
            while(in.hasNext())
            {
            String b = in.next();
            morse[i] = b;
            i++;
            }
            //for(i = 0; i < morse.length; i++)
            return morse;
        }
        public static void  Convert()
        {
          char[] alpha = {'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'};
          int[] num = {0,1,2,3,4,5,6,7,8,9};
          int i = 0;
          for(i = 0; i < alpha.length; i++)
          {
            if(phrase.charAt(i) == alpha[i]);  
          }
     
        }
    }
    My problem is the morse alphabet conversion like a =--. or something to that effect.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Morse Problem

    That's the assignment, but what questions do you have about the code you posted? What problems are you having writing the code to complete the assignment? If you're getting errors, post them. If the results from the code you posted are not correct, show the results, explain what should be different, and ask specific questions about how to fix the code to get the correct results.

  3. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    (I understand the problem but dont see how.)(pseudocode!)See i need the if to be equivalent to asking if the phrase has a letter in the alphabet, return the index. Then look for the index in the norse code adding to new phrase.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Morse Problem

    So try coding that.

  5. #5
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    It wont work because everytime I would go to next letter in phrase it can only be compared to next letter in array.

    --- Update ---

    Here is what I have (tried...)
    /**
     * Write a description of class MorseCode here.
     * 
     * @author (Landon L) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class MorseCode 
    {
        private static String[] morse; 
        private static String phrase;
        private static String Newphrase;
        /**
         * Constructor for objects of class MorseCode
         */
        public MorseCode(String p)
        {
            morse = new String[36];
            phrase = p;
            Newphrase = "";
        }
     
        public static String[] read() throws IOException
        {
            Scanner in = new Scanner(new File("morsecode.txt"));
            String a = phrase;
            int i = 0;
            while(in.hasNext())
            {
            String b = in.next();
            morse[i] = b;
            i++;
            }
            //for(i = 0; i < morse.length; i++)
            return morse;
        }
        public static void  Convert()
        {
          char[] alpha = {'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'};
          int[] num = {0,1,2,3,4,5,6,7,8,9};
          int i = 0;
          String l = "";
          for(i = 0; i < alpha.length; i++)
          {
            if(phrase.charAt(i) == alpha[i]); 
               { 
                l = morse[i];
                Newphrase += l;
               }
          }
     
        }
        public static String getNewPhrase()
        {
            return Newphrase;
        }
    }

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Morse Problem

    Wow, it's hard to get info from you, but we might finally be getting somewhere.

    What I think you've said is that each time you advance to the next letter of the input, the array used for comparison needs to reset to the first element. That means that the same index, i, can't be used for the charAt() method and the alpha[] index. Do you see a way to use two variables? It may take 2 for loops, or it may take an independent index variable that can be reset to zero when needed.

  7. #7
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    is this what you mean
     
    /**
     * Write a description of class MorseCode here.
     * 
     * @author (Landon L) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class MorseCode 
    {
        private static String[] morse; 
        private static String phrase;
        private static String Newphrase;
        /**
         * Constructor for objects of class MorseCode
         */
        public MorseCode(String p)
        {
            morse = new String[36];
            phrase = p;
            Newphrase = "";
        }
     
        public static String[] read() throws IOException
        {
            Scanner in = new Scanner(new File("morsecode.txt"));
            String a = phrase;
            int i = 0;
            while(in.hasNext())
            {
            String b = in.next();
            morse[i] = b;
            i++;
            }
            //for(i = 0; i < morse.length; i++)
            return morse;
        }
        public static void  Convert()
        {
          char[] alpha = {'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'};
          int[] num = {0,1,2,3,4,5,6,7,8,9};
          int i = 0;
          String l = "";
          for(i = 0; i < phrase.length(); i++)
          {  
              for(i = 0; i < alpha.length; i++)
             {   
                 if(phrase.charAt(i) == alpha[i]); 
                    { 
                     l = morse[i];
                     Newphrase += l;
                    }
             }
     
        }
        }
        public static String getNewPhrase()
        {
            return Newphrase;
        }
    }
    This all I could come up with.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Morse Problem

    not the same, two variables = DIFFERENT

    Use a DIFFERENT index/loop control variable in each for loop.

  9. #9
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    /**
     * Write a description of class MorseCode here.
     * 
     * @author (Landon L) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class MorseCode 
    {
        private static String[] morse; 
        private static String phrase;
        private static String Newphrase;
        /**
         * Constructor for objects of class MorseCode
         */
        public MorseCode(String p)
        {
            morse = new String[36];
            phrase = p;
            Newphrase = "";
        }
     
        public static String[] read() throws IOException
        {
            Scanner in = new Scanner(new File("morsecode.txt"));
            String a = phrase;
            int i = 0;
            while(in.hasNext())
            {
            String b = in.next();
            morse[i] = b;
            i++;
            }
            //for(i = 0; i < morse.length; i++)
            return morse;
        }
        public static void  Convert()
        {
          char[] alpha = {'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'};
          int[] num = {0,1,2,3,4,5,6,7,8,9};
          int i = 0;
          int a = 0;
          String l = "";
          for(i = 0; i < phrase.length(); i++)
          {  
              for(a = 0; a < alpha.length; a++)
             {   
                 if(phrase.charAt(i) == alpha[a]); 
                    { 
                     l = morse[a];
                     Newphrase += l;
                    }
             }
     
        }
        }
        public static String getNewPhrase()
        {
            return Newphrase;
        }
    }
    this

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Morse Problem

    Think it through. In plain language, you might say what you're trying to do:

    Get a value from the input
    Compare the value to each element of the array alpha[] to find a match

    So, program that:

    // get a value from the input
    value = charAt( i );
     
    // compare the value to each element of the array alpha[] to find a match
    for ( j = 0 ; j < alpha.length ; j++ )
    {
        if ( value == alpha[j] )
        {
            // then do something
        }
    }
    The for loop I wrote is the inner or nested loop. The outside loop that you'll write (you already have) takes each value of the input one at a time. Write the outer for loop and put it around the for loop I wrote.

  11. #11
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    After making suggested changes here is what I have.
    /**
     * Write a description of class MorseCode here.
     * 
     * @author (Landon L) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class MorseCode 
    {
        private static String[] morse; 
        private static String phrase;
        private static String Newphrase;
        /**
         * Constructor for objects of class MorseCode
         */
        public MorseCode(String p)
        {
            morse = new String[36];
            phrase = p;
            Newphrase = "";
        }
     
        public static String[] read() throws IOException
        {
            Scanner in = new Scanner(new File("morsecode.txt"));
            String a = phrase;
            int i = 0;
            while(in.hasNext())
            {
            String b = in.next();
            morse[i] = b;
            i++;
            }
            //for(i = 0; i < morse.length; i++)
            return morse;
        }
        public static void  Convert()
        {
          char[] alpha = {'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'};
          int[] num = {0,1,2,3,4,5,6,7,8,9};
          int i = 0;
          int a = 0;
          char l;
          for(i = 0; i < phrase.length(); i++)
          {  
            l = phrase.charAt(i);
              for ( a = 0 ; a < alpha.length ; a++ )
              {
                   if ( l == alpha[a] )
                       {
                         Newphrase+= morse[a];
                      }
              }
     
        }
        }
        public static String getNewPhrase()
        {
            return Newphrase;
        }
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Morse Problem

    Does it execute and give you the desired results?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    yes at least for letters I suppose Ill do the same thing for numbers

    --- Update ---

    Now the number part wont return anything
    /**
     * Write a description of class MorseCode here.
     * 
     * @author (Landon L) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class MorseCode 
    {
        private static String[] morse; 
        private static String phrase;
        private static String Newphrase;
        /**
         * Constructor for objects of class MorseCode
         */
        public MorseCode(String p)
        {
            morse = new String[36];
            phrase = p;
            Newphrase = "";
        }
     
        public static String[] read() throws IOException
        {
            Scanner in = new Scanner(new File("morsecode.txt"));
            String a = phrase;
            int i = 0;
            while(in.hasNext())
            {
            String b = in.next();
            morse[i] = b;
            i++;
            }
            //for(i = 0; i < morse.length; i++)
            return morse;
        }
        public static void  Convert()
        {
          char[] alpha = {'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'};
          int[] num = {0,1,2,3,4,5,6,7,8,9};
          int i = 0;
          int a = 0;
          char l;
          int b = 0;
          phrase.toLowerCase();
          for(i = 0; i < phrase.length(); i++)
          {  
            l = phrase.charAt(i);
              for ( a = 0 ; a < alpha.length ; a++ )
              {
                   if ( l == alpha[a] )
                       {
                         Newphrase+= morse[a];
                      }
              }
     
          }
          for(i = 0; i < phrase.length(); i++)
          {  
            b = phrase.charAt(i);
              for ( a = 0 ; a < num.length ; a++ )
              {
                   if ( b == num[a] )
                       {
                         Newphrase+= morse[a];
                      }
              }
     
        }
        }
        public static String getNewPhrase()
        {
            return Newphrase;
        }
    }
    Here is tester.
    /**
     * Write a description of class MorseCodeTester here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.io.*;
    public class MorseCodeTester
    {
       public static void main(String [] args) throws IOException
       { 
           MorseCode m = new MorseCode("Hohoho");
           m.read();
           m.Convert();
           System.out.println(m.getNewPhrase());
        }
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Morse Problem

    Please copy and post the console contents that shows what was input to the program and what it printed out.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    Input was Hohoho122.
    Output was ---....---....---

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Morse Problem

    Looks like the output is missing the leading h and the ending numbers.

    Try debugging the code by adding some println statements that print out the values of variables as they are changed so you can see what the computer sees as the code executes. If you understand what the code is supposed to do, seeing what it is actually doing will help you fix the problem.
    Be sure to add id Strings when printing so you know what was printed. For example when printing an array:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    /**
     * Write a description of class MorseCode here.
     * 
     * @author (Landon L) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class MorseCode 
    {
        private static String[] morse; 
        private static String phrase;
        private static String Newphrase;
        /**
         * Constructor for objects of class MorseCode
         */
        public MorseCode(String p)
        {
            morse = new String[36];
            phrase = p;
            Newphrase = "";
        }
     
        public static String[] read() throws IOException
        {
            Scanner in = new Scanner(new File("morsecode.txt"));
            String a = phrase;
            int i = 0;
            while(in.hasNext())
            {
            String b = in.next();
            morse[i] = b;
            i++;
            }
            //for(i = 0; i < morse.length; i++)
            return morse;
        }
        public static void  Convert()
        {
          char[] alpha = {'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'};
          int[] num = {0,1,2,3,4,5,6,7,8,9};
          int i = 0;
          int a = 0;
          char l;
          int b = 0;
          phrase  = phrase.toLowerCase();
          for(i = 0; i < phrase.length(); i++)
          {  
            l = phrase.charAt(i);
              for ( a = 0 ; a < alpha.length ; a++ )
              {
                   if ( l == alpha[a] )
                       {
                         Newphrase+= morse[a];
                        }
              }
     
          }
          for(i = 0; i < phrase.length(); i++)
          {  
            b = phrase.charAt(i);
              for ( a = 0 ; a < num.length ; a++ )
              {
                   if ( b == num[a] )
                       {
                         Newphrase+= morse[a];
                      }
              }
     
          }
        }
        public static String getNewPhrase()
        {
            return Newphrase;
        }
    }
    Now the H prints but still even with the Id statements, nothing wrong with array printed so thats not it.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Morse Problem

    All the input are char values. None of the input is int. The char: '1' or the String: "1" does not have the same value as the int: 1.
    To see the values of the characters, look at an ASCII character table.

    The code should also print out an error message when it tries to convert a char and does NOT find a match for it. That will require using a boolean variable that can remember if a match was found. At the end of the search loop test the value of the boolean and print an error message saying the char was not found.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    but the code has to convert ints to morse code also.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Morse Problem

    '1' is a char and "1" is a String. Their values are not the same as the int: 1.

    The charAt() method returns a char like '1', it does NOT return an int value like 1.

    Change the code to work with char values, not int values.

    Did you look at the ASCII character table values? For example, copy and execute this:
          System.out.println("char 1 value is="+(int)'1');
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    Thanks finally fixed the problem or at least I think I did based on results
     
    /**
     * Write a description of class MorseCode here.
     * 
     * @author (Landon L) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class MorseCode 
    {
        private static String[] morse; 
        private static String phrase;
        private static String Newphrase;
        /**
         * Constructor for objects of class MorseCode
         */
        public MorseCode(String p)
        {
            morse = new String[36];
            phrase = p;
            Newphrase = "";
        }
     
        public static String[] read() throws IOException
        {
            Scanner in = new Scanner(new File("morsecode.txt"));
            String a = phrase;
            int i = 0;
            while(in.hasNext())
            {
            String b = in.next();
            morse[i] = b;
            i++;
            }
            //for(i = 0; i < morse.length; i++)
            return morse;
        }
        public static void  Convert()
        {
          char[] alpha = {'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'};
          char[] num = {'0','1','2','3','4','5','6','7','8','9'};
          int i = 0;
          int a = 0;
          char l;
          int b = 0;
          phrase  = phrase.toLowerCase();
          for(i = 0; i < phrase.length(); i++)
          {  
            l = phrase.charAt(i);
              for ( a = 0 ; a < alpha.length ; a++ )
              {
                   if ( l == alpha[a] )
                       {
                         Newphrase+= morse[a];
                        }
              }
     
          }
          for(i = 0; i < phrase.length(); i++)
          {  
            b = phrase.charAt(i);
              for ( a = 0 ; a < num.length ; a++ )
              {
                   if ( b == num[a] )
                       {
                         Newphrase+= "    " + morse[a+25];
                      }
              }
     
          }
        }
        public static String getNewPhrase()
        {
            return Newphrase;
        }
    }
    Space was removed but used to test results.

    --- Update ---

    Now my problem is spacing when a sentence is inserted
     
    /**
     * Write a description of class MorseCode here.
     * 
     * @author (Landon L) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class MorseCode 
    {
        private static String[] morse; 
        private static String phrase;
        private static String Newphrase;
        /**
         * Constructor for objects of class MorseCode
         */
        public MorseCode(String p)
        {
            morse = new String[36];
            phrase = p;
            Newphrase = "";
        }
     
        public static String[] read() throws IOException
        {
            Scanner in = new Scanner(new File("morsecode.txt"));
            String a = phrase;
            int i = 0;
            while(in.hasNext())
            {
            String b = in.next();
            morse[i] = b;
            i++;
            }
            //for(i = 0; i < morse.length; i++)
            return morse;
        }
        public static void  Convert()
        {
          char[] alpha = {'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'};
          char[] num = {'0','1','2','3','4','5','6','7','8','9'};
          int i = 0;
          int a = 0;
          char l;
          int b = 0;
          phrase  = phrase.toLowerCase();
          for(i = 0; i < phrase.length(); i++)
          {  
            l = phrase.charAt(i);
              if((int) 'l' == 32)
                   Newphrase += " ";
              for ( a = 0 ; a < alpha.length ; a++ )
              {
                   if ( l == alpha[a] )
                       {
                         Newphrase += morse[a];
                        }
              }
     
          }
          for(i = 0; i < phrase.length(); i++)
          {  
            b = phrase.charAt(i);
              for ( a = 0 ; a < num.length ; a++ )
              {
                   if ( b == num[a] )
                       {
                         Newphrase+= morse[a+25];
                      }
              }
     
          }
        }
        public static String getNewPhrase()
        {
            return Newphrase;
        }
    }
    for example inputting I love
    would printout all morse code letters correctly in one jumble.

    --- Update ---

    So go ahead and scrap what I just said (Spacing still doesnt work also found out numbers between letters dont pint out until after letters are printing which makes sense because of my organization. Now with program reorganized they dont print at all.)
     
    /**
     * Write a description of class MorseCode here.
     * 
     * @author (Landon L) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class MorseCode 
    {
        private static String[] morse; 
        private static String phrase;
        private static String Newphrase;
        /**
         * Constructor for objects of class MorseCode
         */
        public MorseCode(String p)
        {
            morse = new String[36];
            phrase = p;
            Newphrase = "";
        }
     
        public static String[] read() throws IOException
        {
            Scanner in = new Scanner(new File("morsecode.txt"));
            String a = phrase;
            int i = 0;
            while(in.hasNext())
            {
            String b = in.next();
            morse[i] = b;
            i++;
            }
            //for(i = 0; i < morse.length; i++)
            return morse;
        }
        public static void  Convert()
        {
          char[] alpha = {'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'};
          char[] num = {'0','1','2','3','4','5','6','7','8','9'};
          int i = 0;
          int a = 0;
          char l;
          int b = 0;
          phrase  = phrase.toLowerCase();
          for(i = 0; i < phrase.length(); i++)
          {  
            l = phrase.charAt(i);
            if((int) 'l' == 32)
                   Newphrase += " ";
              else if(((int) 'l'>= 99)&& ((int) 'l'<= 122))
              {
              for ( a = 0 ; a < alpha.length ; a++ )
              {
                   if ( l == alpha[a] )
                       {
                         Newphrase += morse[a];
                        }
     
              }
              }
              else if(((int) 'l'>= 48)&& ((int) 'l'<= 57))
              {
              for ( a = 0 ; a < num.length ; a++ )
              {
                   if ( l == num[a] )
                       {
                         Newphrase+= morse[a+25];
                      }
              }
            }
          }
        }
        public static String getNewPhrase()
        {
            return Newphrase;
        }
    }

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Morse Problem

    The code doesn't appear to be testing the value of the char variable assigned the value returned by charAt().
    BTW l is a very poor variable name. It looks too much like the digit 1. In general single letter variable names are ONLY used in for loops for the looping index.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Morse Problem

    I dont understand what is meant by your first sentence/how to fix it.

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Morse Problem

       l = phrase.charAt(i);
    First you need to change the name of the variable that is assigned the value returned by the charAt() method.
    l is not a good variable name. It is hard to talk about the variable l as it gets lost and confused too easily in a post and it is impossible to do a Find in the program for that variable name as that single letter occurs in too many places.

    Change it to something like: nextChar
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Morse Code Conversion Problems
    By SillyLilly in forum What's Wrong With My Code?
    Replies: 13
    Last Post: August 30th, 2013, 02:02 AM
  2. Morse Code
    By m49er704 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 24th, 2013, 06:48 AM
  3. morse code
    By daz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 8th, 2012, 05:50 AM
  4. [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
  5. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM