Results 1 to 20 of 26
Thread: Morse Code Help Please!!
- 08-16-2012, 04:36 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
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;
// }
}
- 08-16-2012, 04:38 AM #2
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-16-2012, 04:40 AM #3
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
Re: Morse Code Help Please!!
well im getting missing return statement for lines 30 and 39 even though i think theyre ok. thanks!
- 08-16-2012, 04:42 AM #4
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-16-2012, 04:43 AM #5
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
Re: Morse Code Help Please!!
wait what? in addition to being new to java i am new to this website as well!! sorry!
- 08-16-2012, 04:44 AM #6
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
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??
- 08-16-2012, 04:54 AM #7
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-17-2012, 03:41 AM #8
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
Re: Morse Code Help Please!!
oh ok thank you! so should i put an else statement that also returns something in #2??
- 08-17-2012, 03:44 AM #9
Re: Morse Code Help Please!!
Why not try it and see?
- 08-17-2012, 03:54 AM #10
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
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; // } }
- 08-17-2012, 03:58 AM #11
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-17-2012, 04:00 AM #12
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
Re: Morse Code Help Please!!
Project1CTY.java:42: error: missing return statement
}
^
1 error
- 08-17-2012, 04:07 AM #13
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-17-2012, 04:09 AM #14
Re: Morse Code Help Please!!
Your returns statements are inside a loop. What gets returned if the loop is never entered?
- 08-17-2012, 04:09 AM #15
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
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?
- 08-17-2012, 04:12 AM #16
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Morse Code Help Please!!
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-17-2012, 04:12 AM #17
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
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!
- 08-17-2012, 04:14 AM #18
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
- 08-17-2012, 04:16 AM #19
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
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!!
- 08-17-2012, 04:21 AM #20
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.
Similar Threads
-
Morse code translation?
By Koba in forum New To JavaReplies: 6Last Post: 10-09-2011, 11:48 PM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
Morse Code Java Program - Help Needed Please!!
By dungeondragon in forum New To JavaReplies: 8Last Post: 02-25-2011, 04:36 AM -
Morse Code
By Jamison5213 in forum New To JavaReplies: 6Last Post: 04-14-2010, 04:56 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


1Likes
LinkBack URL
About LinkBacks


Bookmarks