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

Thread: Need Help With Project

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help With Project

    I am doing a project that converts a string into morse code. I have most of it down except for that fact that I cannot get it to keep repeating so I tried inserting another while loop in the main method but i keep getting errors with my code and I need to insert a | in between each space but I have been unable to do that either. The following is my code so far if anyone has any tips I would greatly appreciate it.
    import java.util.*;
     
     
    public class MorseCodeProj
    {
      public String Conversion(String s1){
        String result = "";
        StringTokenizer tokens = new StringTokenizer(s1);
        while(tokens.hasMoreTokens()){  
         String word = tokens.nextToken();
        for (int i = 0; i < word.length(); i++){
            switch(word.charAt(i)){
                case 'A':
                case 'a':
                    result+=".-";
                    break;
                case 'B':
                case 'b':
                    result+="-...";
                    break;
                case 'C':
                case 'c':
                    result+="-.-.";
                    break;
                case 'D':
                case 'd':
                    result+="-..";
                    break;
                case 'E':
                case 'e':
                    result+=".";
                    break;
                case 'F':
                case 'f':
                    result+="..-.";
                    break;
                case 'G':
                case 'g':
                    result+="--.";
                    break;
                case 'H':
                case 'h':
                    result+="....";
                    break;
                case 'I':
                case 'i':
                    result+="..";
                    break;
                case 'J':
                case 'j':
                    result+=".---";
                    break;
                case 'K':
                case 'k':
                    result+="-.-";
                    break;
                case 'L':
                case 'l':
                    result+=".-..";
                    break;
                case 'M':
                case 'm':
                    result+="--";
                    break;
                case 'N':
                case 'n':
                    result+="-.";
                    break;
                case 'O':
                case 'o':
                    result+="---";
                    break;
                case 'P':
                case 'p':
                    result+=".--.";
                    break;
                case 'Q':
                case 'q':
                    result+="--.-";
                    break;
                case 'R':
                case 'r':
                    result+=".-.";
                    break;
                case 'S':
                case 's':
                    result+="...";
                    break;
                case 'T':
                case 't':
                    result+="-";
                    break;
                case 'U':
                case 'u':
                    result+="..-";
                    break;
                case 'V':
                case 'v':
                    result+="...-";
                    break;
                case 'W':
                case 'w':
                    result+=".--";
                    break;
                case 'X':
                case 'x':
                    result+="-..-";
                    break;
                case 'Y':
                case 'y':
                    result+="-.--";
                    break;
                case 'Z':
                case 'z':
                    result+="--..";
                    break;
                case 1 : 
                    result+=".----";
                    break;
                case 2 :
                    result+="..---";
                    break;
                case 3 :
                    result+="...--";
                    break;
                case 4 :
                    result+="....-";
                    break;
                case 5 :
                    result+=".....";
                    break;
                case 6 :
                    result+="-....";
                    break;
                case 7 :
                    result+="--...";
                    break;
                case 8 :
                    result+="---..";
                    break;
                case 9 :
                    result+="----.";
                    break;
                case 0 :
                    result+="-----";
                    break;
     
     
        }
     
     
    }
     
        }
        return result;
    }
        public void mainMethod(){
        Scanner input = new Scanner(System.in);
        boolean DONE = true;
     
        while ( s1!equal(DONE)){
     
        System.out.println("Enter line of characters.");  
            String s1 = input.nextLine();
            System.out.println("Converted String : " + Conversion(s1));
     
        return false;
    }
     
     
     
        }
     
    }


  2. #2
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default Re: Need Help With Project

    I just edited my post incase you read it a few moments ago

    import java.util.*;
     
     
    public class MorseCodeProj
    {
      public String Conversion(String s1){
        String result = "";
        StringTokenizer tokens = new StringTokenizer(s1);
        while(tokens.hasMoreTokens()){
         String word = tokens.nextToken();
        for (int i = 0; i < word.length(); i++){
            switch(word.charAt(i)){
                case 'A':
                case 'a':
                    result+=".-";
                    break;
                case 'B':
                case 'b':
                    result+="-...";
                    break;
                case 'C':
                case 'c':
                    result+="-.-.";
                    break;
                case 'D':
                case 'd':
                    result+="-..";
                    break;
                case 'E':
                case 'e':
                    result+=".";
                    break;
                case 'F':
                case 'f':
                    result+="..-.";
                    break;
                case 'G':
                case 'g':
                    result+="--.";
                    break;
                case 'H':
                case 'h':
                    result+="....";
                    break;
                case 'I':
                case 'i':
                    result+="..";
                    break;
                case 'J':
                case 'j':
                    result+=".---";
                    break;
                case 'K':
                case 'k':
                    result+="-.-";
                    break;
                case 'L':
                case 'l':
                    result+=".-..";
                    break;
                case 'M':
                case 'm':
                    result+="--";
                    break;
                case 'N':
                case 'n':
                    result+="-.";
                    break;
                case 'O':
                case 'o':
                    result+="---";
                    break;
                case 'P':
                case 'p':
                    result+=".--.";
                    break;
                case 'Q':
                case 'q':
                    result+="--.-";
                    break;
                case 'R':
                case 'r':
                    result+=".-.";
                    break;
                case 'S':
                case 's':
                    result+="...";
                    break;
                case 'T':
                case 't':
                    result+="-";
                    break;
                case 'U':
                case 'u':
                    result+="..-";
                    break;
                case 'V':
                case 'v':
                    result+="...-";
                    break;
                case 'W':
                case 'w':
                    result+=".--";
                    break;
                case 'X':
                case 'x':
                    result+="-..-";
                    break;
                case 'Y':
                case 'y':
                    result+="-.--";
                    break;
                case 'Z':
                case 'z':
                    result+="--..";
                    break;
                case 1 : 
                    result+=".----";
                    break;
                case 2 :
                    result+="..---";
                    break;
                case 3 :
                    result+="...--";
                    break;
                case 4 :
                    result+="....-";
                    break;
                case 5 :
                    result+=".....";
                    break;
                case 6 :
                    result+="-....";
                    break;
                case 7 :
                    result+="--...";
                    break;
                case 8 :
                    result+="---..";
                    break;
                case 9 :
                    result+="----.";
                    break;
                case 0 :
                    result+="-----";
                    break;
     
     
        }
     
    [B]    if(tokens.hasMoreTokens())
        result += " ";[/B]
    //I put a space there so it adds a space in case the statement entered is multiple words?
    }
     
        }
        return result;
    }
        public void mainMethod(){
        Scanner input = new Scanner(System.in);
     
    // No boolean variable needed.
    [B]    String s1;[/B]
        do{
            System.out.println("Enter line of characters.");  
            s1 = input.nextLine();
     
            if(!s1.equalsIgnoreCase("DONE"))
            System.out.println("Converted String : " + Conversion(s1));
            //the If statement is there so that we do not translate the word "done" when we exit.
     
    //WHY did you have return true here?
        }while ( !s1.equalsIgnoreCase("DONE"));
    //You cant test s1 before you even assigned it...replaced with do-while loop, now you can assign value to s1 BEFORE YOU TEST what it holds.
     
    //the code now will continue asking for more till the word "done" is entered
     
     
     
        }
     
    }
    Last edited by rsala004; November 11th, 2009 at 05:24 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Need Help With Project

    As an aside from a quick glance at the code, you may want to consider using a StringBuilder or StringBuffer for more optimal performance. Relatively speaking String concatenation can be slow, and for longer strings this difference could be noticeable.

Similar Threads

  1. Project Euler
    By helloworld922 in forum The Cafe
    Replies: 5
    Last Post: September 9th, 2009, 02:51 PM
  2. Need a project
    By helloworld922 in forum Project Collaboration
    Replies: 6
    Last Post: July 31st, 2009, 08:30 AM
  3. Adventure ride project
    By Ciwan in forum Java Theory & Questions
    Replies: 12
    Last Post: February 15th, 2009, 11:08 AM
  4. Database connection without using netbease
    By haygaurav in forum Java IDEs
    Replies: 9
    Last Post: December 9th, 2008, 06:14 AM
  5. Replies: 4
    Last Post: May 22nd, 2008, 10:59 AM