Page 1 of 2 12 LastLast
Results 1 to 20 of 26
Like Tree1Likes

Thread: Morse Code Help Please!!

  1. #1
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Morse Code Help Please!!

    hi im new to java and i just need some help on this program!! im just focusing on converting morse to english right now so thats why there are comment lines. could you help?? thanks!

    public class Project1CTY {
    public static void main( String [] args ) {
    int answer = Input.getInt( "If you wish to translate Morse Code into English, enter the number 0, \n if you wish to translate English into Morse Code, enter the number 1: ");

    if (answer == 0) {
    String x = Input.getString("You chose to translate Morse Code into English. Input the Code below. \n When inputting Morse Code, separate each letter/digit with a single space, and delimit multiple words with a “|”. \n For example, - --- | -... . would be the Morse Code input for the sentence “to be”.");
    MorseConvert(x);
    }
    // else if(answer == 1) {
    // String y = Input.getString("You chose to translate English into Morse Code. Enter the sentence you wish to convert below. \n When inputting English, separate each word with a blank space.");
    // EnglishConvert(y);
    // }
    else
    System.out.println( "Invalid Entry" );
    }

    public static String MorseConvert(String x) {
    String[] morseArray1 = x.split("|");
    for(int i = 0; i < morseArray1.length; i++) {
    String[] morseArray2 = morseArray1[i].split(" ");
    for(int j = 0; j < morseArray2.length; j++) {
    if (morsedatabase( morseArray2[j] ) <= 25) {
    letter(morsedatabase( morseArray2[j]));
    }
    else {
    number(morsedatabase(morseArray2[j]));
    }
    }
    }
    }

    public static int morsedatabase( String a ) {
    String[] morseData = {".-","-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
    for (int p = 0; p < morseData.length; p++) {
    if (morseData[p].equals(a)) {
    return p;
    }
    }
    }
    public static char letter(int m) {
    char[] 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'};
    return letters[m];
    }

    public static int number(int m) {
    char[] numbers = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    return numbers[m];
    }

    // public static String EnglishConvert(String y) {
    // String z = y.toLowerCase;
    // }
    }

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Morse Code Help Please!!

    Do you have a specific question? Are you receiving any errors?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    well im getting missing return statement for lines 30 and 39 even though i think theyre ok. thanks!

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Morse Code Help Please!!

    Can you please surround your code with [code][/code] tags? It will make it easier for people to read.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    wait what? in addition to being new to java i am new to this website as well!! sorry!

  6. #6
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    Java Code:
    public class Project1CTY {
    	public static void main( String [] args ) {
    		int answer = Input.getInt( "If you wish to translate Morse Code into English, enter the number 0, \n if you wish to translate English into Morse Code, enter the number 1: ");
    
    		if (answer == 0) {
    			String x = Input.getString("You chose to translate Morse Code into English. Input the Code below. \n When inputting Morse Code, separate each letter/digit with a single space, and delimit multiple words with a “|”. \n For example, - --- | -... . would be the Morse Code input for the sentence “to be”.");
    			MorseConvert(x);
    		}
    	//	else if(answer == 1) {
    	//		String y = Input.getString("You chose to translate English into Morse Code. Enter the sentence you wish to convert below. \n When inputting English, separate each word with a blank space.");
    	//		EnglishConvert(y);
    	//	}
    		else
    			System.out.println( "Invalid Entry" );
    	}
    
    	public static String MorseConvert(String x) {
    		String[] morseArray1 = x.split("|");
    		for(int i = 0; i < morseArray1.length; i++) {
    			String[] morseArray2 = morseArray1[i].split(" ");
    			for(int j = 0; j < morseArray2.length; j++) {
    				if (morsedatabase( morseArray2[j] ) <= 25) {
    					letter(morsedatabase( morseArray2[j]));
    				}
    				else {
    					number(morsedatabase(morseArray2[j]));
    				}
    			}
    		}
    	}
    
    	public static int morsedatabase( String a ) {
    		String[] morseData = {".-","-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
    		for (int p = 0; p < morseData.length; p++) {
    			if (morseData[p].equals(a)) {
    				return p;
    			}
    		}
    	}
    	public static char letter(int m) {
    		char[] 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'};
    		return letters[m];
    	}
    
    	public static int number(int m) {
    		char[] numbers = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    		return numbers[m];
    	}
    
    //	public static String EnglishConvert(String y) {
    //		String z = y.toLowerCase;
    //	}
    }


    like so??
    awinston likes this.

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Morse Code Help Please!!

    1. You specify MorseConvert() as having a return of type String, but you don't return anything in that method.

    2. morsedatabase() only returns an int if a condition is satisfied (morseData[p].equals(a)), and the compiler will have a problem with this type of conditional return. The reason for this is because it is possible for the condition to be false, and this would result in nothing being returned.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  8. #8
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    oh ok thank you! so should i put an else statement that also returns something in #2??

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

    Default Re: Morse Code Help Please!!

    Why not try it and see?

  10. #10
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    it still doesn't work :( but at least i am only getting an error for line 42!! (#2). here is what i have right now.
    Java Code:
    public class Project1CTY {
    	public static void main( String [] args ) {
    		int answer = Input.getInt( "If you wish to translate Morse Code into English, enter the number 0, \n if you wish to translate English into Morse Code, enter the number 1: ");
    
    		if (answer == 0) {
    			String x = Input.getString("You chose to translate Morse Code into English. Input the Code below. \n When inputting Morse Code, separate each letter/digit with a single space, and delimit multiple words with a “|”. \n For example, - --- | -... . would be the Morse Code input for the sentence “to be”.");
    			MorseConvert(x);
    		}
    	//	else if(answer == 1) {
    	//		String y = Input.getString("You chose to translate English into Morse Code. Enter the sentence you wish to convert below. \n When inputting English, separate each word with a blank space.");
    	//		EnglishConvert(y);
    	//	}
    		else
    			System.out.println( "Invalid Entry" );
    	}
    
    	public static void MorseConvert(String x) {
    		String[] morseArray1 = x.split("|");
    		for(int i = 0; i < morseArray1.length; i++) {
    			String[] morseArray2 = morseArray1[i].split(" ");
    			for(int j = 0; j < morseArray2.length; j++) {
    				int w = morsedatabase(morseArray2[j]);
    				if (morsedatabase( morseArray2[j] ) <= 25) {
    					System.out.print(letter(w));
    				}
    				else {
    					System.out.print(number(w));
    				}
    			}
    		}
    	}
    
    	public static int morsedatabase( String a ) {
    		String[] morseData = {".-","-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
    		for (int p = 0; p < morseData.length; p++) {
    			if (morseData[p].equals(a)) {
    				return p;
    			}
    			else 
    				return 100;
    		}
    	}
    
    	public static char letter(int m) {
    		char[] 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'};
    		return letters[m];
    	}
    
    	public static int number(int m) {
    		char[] numbers = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    		return numbers[m];
    	}
    
    //	public static String EnglishConvert(String y) {
    //		String z = y.toLowerCase;
    //	}
    }

  11. #11
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Morse Code Help Please!!

    Can you post the error message that you are getting?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  12. #12
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    Project1CTY.java:42: error: missing return statement
    }
    ^
    1 error

  13. #13
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Morse Code Help Please!!

    Here's the question: what would happen if morseData was an empty array?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

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

    Default Re: Morse Code Help Please!!

    Your returns statements are inside a loop. What gets returned if the loop is never entered?

  15. #15
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    i am sorry i am not sure what you mean by that. could you perhaps edit part of the code to help me understand?

  16. #16
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Morse Code Help Please!!

    Quote Originally Posted by javaa View Post
    i am sorry i am not sure what you mean by that. could you perhaps edit part of the code to help me understand?
    The execution of your loop is dependent on the length of array morseData. If morseData is empty, the loop is never executed and therefore nothing is returned. The compiler is worried about that.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  17. #17
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    oh ok never mind i got the code to have no errors but now it just returns of letters that seem to be random but at least there are no errors!! thanks guys for helping me im really new so i get a lot of trouble. youre the best!

  18. #18
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Morse Code Help Please!!

    Quote Originally Posted by javaa View Post
    oh ok never mind i got the code to have no errors but now it just returns of letters that seem to be random but at least there are no errors!! thanks guys for helping me im really new so i get a lot of trouble. youre the best!
    Hang on, hang on. This is not good if you do not understand what is going on.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  19. #19
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    oh... haha hmmm well im not really sure what to do but maybe converting english to morse code is easier so i might try that. thanks so much!!

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

    Default Re: Morse Code Help Please!!

    The best solution would be to have a return statement as the last line of your method. That way no matter what path it takes through your method it MUST hit the return statement. What you can do is use a StringBuilder object to add you code elements to within loops and if statements or whatever and then return the String at the end.

Page 1 of 2 12 LastLast

Similar Threads

  1. Morse code translation?
    By Koba in forum New To Java
    Replies: 6
    Last Post: 10-10-2011, 12:48 AM
  2. Replies: 0
    Last Post: 08-07-2011, 09:32 PM
  3. Morse Code Java Program - Help Needed Please!!
    By dungeondragon in forum New To Java
    Replies: 8
    Last Post: 02-25-2011, 05:36 AM
  4. Morse Code
    By Jamison5213 in forum New To Java
    Replies: 6
    Last Post: 04-14-2010, 05:56 AM
  5. Replies: 1
    Last Post: 04-26-2007, 04: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
  •