Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (davidusa):

JAVA PROGRAMMING HELP

OpenStudy (davidusa):

thanks im gonna post question now oops lol

OpenStudy (davidusa):

I am making a Java program that translates from Morse code to English and vice versa. However, only English to Morse code works, not the other way. The program Compiles.. but doesnt exactly work.

OpenStudy (davidusa):

http://screencast-o-matic.com/watch/c21qbpnCvU

OpenStudy (davidusa):

Do you know Java programming?

OpenStudy (dan815):

show me code

OpenStudy (dan815):

And I know very basics in a lot of random languages, I usually program everything on Python and Matlab, dont deal with heavy syntax problems usually with my kind of questions

OpenStudy (dan815):

how are your parsing your input

OpenStudy (dan815):

@aajugdar java programming

OpenStudy (davidusa):

heres the file

OpenStudy (davidusa):

thanks for coming @aajugdar and @dan815

OpenStudy (dan815):

@dark_riddles we need your professional help @ganeshie8 These guys program for a living

OpenStudy (dan815):

Okay David some qusetions for you

OpenStudy (dan815):

I need to redownload netbeans right now to open java files again

OpenStudy (davidusa):

NO. get jdk

OpenStudy (davidusa):

jdk much better @ganeshie8 thanks for coming 2 help

OpenStudy (dan815):

tell me what you are basically doing for going from morse to english

OpenStudy (dan815):

You have | separting words

OpenStudy (davidusa):

yes...

OpenStudy (dan815):

How do you know where the letter is ending

OpenStudy (davidusa):

there are spaces between letters and | between words

OpenStudy (dan815):

okay

OpenStudy (davidusa):

so it would be like m y | n a m e | i s | d a v i d

OpenStudy (dan815):

and morse with be like -.-- Space .., space....- | .... right?

OpenStudy (davidusa):

i think...

OpenStudy (dan815):

okay well you can split for every | in the input

OpenStudy (dan815):

then split for ever space

OpenStudy (dan815):

and do a loop up for each string now

OpenStudy (davidusa):

yay i reported aadjugar because his post about whatever, and a mod deleted it yay

OpenStudy (davidusa):

hm im not sure what ur sayin

OpenStudy (davidusa):

I'll upload new screencast of my windows command prompt and stuff. u should use windows command prompt and jdk for java its easier and better

OpenStudy (dan815):

are you also goin to put in sound for the morse code?

OpenStudy (dan815):

It would be cool if you made a program that was interpreting the sound and sending the morse code sounds

OpenStudy (dan815):

lemme see who else does java..

OpenStudy (dan815):

@Kainui @wio

OpenStudy (anonymous):

``` public class morsecode { public static void main( String [] args ) { String [] referenceMorse = {".-","-...", "-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",".----","..---","...--","....-","-----","-....","--...","---..","----.","-----","|"}; String [] referenceEnglish = {"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","1","2","3","4","5","6","7","8","9","0"," "}; //all the reference translations String whichone = Input.getString("If you would like to translate English to Morse, type in 'Morse'. \nIf you would like to translate Morse to English, type in 'English'. "); if ( whichone.equals("Morse") ) { toMorse(referenceMorse , referenceEnglish); } if ( whichone.equals("English") ) { toEnglish(referenceMorse , referenceEnglish); } } public static void toMorse( String [] referenceMorse , String [] referenceEnglish ) { String englishinput = Input.getString("Enter the English characters you would like to translate."); System.out.println("\n" + "Your input was: " + englishinput ); System.out.println( ); String english = englishinput.toLowerCase(); int size = english.length(); System.out.println("In Morse, this is:"); for (int i = 0 ; i < size ; i ++ ) { String l; if( i == (size - 1) ) { l = english.substring(i); } else { l = english.substring(i,i+1); } for (int j = 0 ; j < 37 ; j++ ) { if ( l.equals ( referenceEnglish [j])) { System.out.print( referenceMorse [j] + " "); } } } System.out.println(); } public static void toEnglish( String [] referenceMorse , String [] referenceEnglish ) { String morse = Input.getString("Enter the Morse characters you would like to translate, putting vertical bars (|) between words."); System.out.println("\n" + "Your input was: " + morse ); System.out.println( ); int size = morse.length(); int numofSpaces = 0 ; System.out.println("In English, this is:"); int count [] = new int [100]; for(int i = 0 ; i < size ; i++ ) { String s; int r = 0; if ( i == size - 1 ) { s = morse.substring(i); } else { s = morse.substring(i, i +1 ); } if ( s.equals (" ")) { numofSpaces++; r ++ ; } else { count [r] ++; } } for(int i = 0 ; i <= numofSpaces ; i ++ ) { String word [] = new String [(count [i])]; String l; for(int j = 0 ; j < count [i] ; j ++ ) { if ( j == (count[i] - 1) ) { l = morse.substring(j); } else { l = morse.substring(j, j +1 ); } word [j] = l ; } for (int k = 0 ; k < 37 ; k++ ) { if ( word .equals ( referenceMorse [k])) { System.out.println ( referenceEnglish [k] + " "); } } } } } ```

OpenStudy (anonymous):

Do you know how functions work?

OpenStudy (dan815):

wio will own this question

OpenStudy (davidusa):

yes sorry i was gone for a while making vid so u guys can see my problem

OpenStudy (anonymous):

You can use `String[] parts = morse.split(' ');` to split a string `morse` up by the spaces.

OpenStudy (anonymous):

For example: ``` String[] words = "This is a bunch of words".split(' '); for (int i = 0; i < words.length; i++) { System.out.println(words[i]); } ``` Then you will have output: ``` This is a bunch of words ```

OpenStudy (davidusa):

thanks here are the instructions

OpenStudy (davidusa):

@dan815 no i wont make sounds lol sorry look http://screencast-o-matic.com/watch/c21qrrnCyj

OpenStudy (davidusa):

can u here in my vid? im not sure because my sound is muted, but i wanna know

OpenStudy (dan815):

Aww that would have been fun :)

OpenStudy (davidusa):

wow everyone is afk this is... :( :P

OpenStudy (anonymous):

hold on, I'm looking at your code.

OpenStudy (davidusa):

Ok thanks

OpenStudy (davidusa):

@Dark_Riddles Please help java programming

OpenStudy (anonymous):

ok, I got no idea how your code is supposed to work.

OpenStudy (davidusa):

Why is my program not working? Is there a way to make it work without changing it a lot?

OpenStudy (davidusa):

Lol

OpenStudy (anonymous):

You should use: `String[] morseLetters = morse.split(' ');`

OpenStudy (anonymous):

Then go through each letter, looking it up, and printing it out.

OpenStudy (davidusa):

ok thanks

OpenStudy (anonymous):

Sorry, can't use sound right now.

OpenStudy (davidusa):

what would i delete and where would i put the string thing

OpenStudy (kainui):

That's the fun of programming. =P

ganeshie8 (ganeshie8):

you declared `word` as an array, but you're comparing it against a character

OpenStudy (anonymous):

Consider ``` String morseLetters = morse.split(' '); for (int i = 0; i < morseLetters.length(); i++){ String morseLetter = morseLetters[i]; // try to find morseLetter in the morese reference here. } ```

OpenStudy (davidusa):

can u edit my program and send it back because im getting a little confused.

OpenStudy (anonymous):

what I wrote should completely replace your 'toEnglish' function.

OpenStudy (davidusa):

ok. from there to the bottom?

OpenStudy (anonymous):

Yes

OpenStudy (davidusa):

thanks let me try

OpenStudy (anonymous):

except for the print statements

OpenStudy (anonymous):

nope tell me what part of the code you don't get.

OpenStudy (davidusa):

theres an error message....

OpenStudy (anonymous):

no, what I mean is... do you understand what: ``` String morseLetters = morse.split(' '); for (int i = 0; i < morseLetters.length(); i++){ String morseLetter = morseLetters[i]; // try to find morseLetter in the morese reference here. } ``` does?

OpenStudy (anonymous):

okay, how will you fill it out?

OpenStudy (davidusa):

what?

OpenStudy (anonymous):

`// try to find morseLetter in the morse reference here.` This part needs to be replaced with other code.

OpenStudy (davidusa):

i have no idea.

OpenStudy (davidusa):

GAAHHH @WIO HELP!

OpenStudy (davidusa):

@iambatman help me

OpenStudy (anonymous):

.... no you need to go through the morse reverence array, then check if the the entry equals the morse letter.

OpenStudy (anonymous):

Did you write any of the code you had originally...?

OpenStudy (davidusa):

finally hi dan

OpenStudy (dan815):

Yes? What can I do, i dont know Java syntax

OpenStudy (dan815):

wio has you covered

OpenStudy (anonymous):

David... what is the problem? You are not explaining where you are stuck.

OpenStudy (anonymous):

Did you come just to get an answer?

OpenStudy (kainui):

@DavidUsa There's really only one way to learn code, and that is to understand it. I can help you, but I wouldn't be able to tell you more than @wio is saying. You have to ask questions about what you are having trouble with. If you don't understand the entire program, then start from the beginning and follow it through. Start at the main method and follow through your code imagining what should happen at every line. Does it do what you think it does? Play with it to make sure. Make smaller programs to test only certain parts of code you don't understand completely to make sure. Try to see if messes up like you expect. Play with it and push the boundaries on what you think things do. Yes, you are not as close as you think you are to finishing this program. But that's ok; if you learn things now then more programming will become easier for you in the future and be much faster. You have to walk before you can run.

OpenStudy (anonymous):

David, you aren't asking a single question. That is why you aren't learning.

OpenStudy (davidusa):

wait what i was afk what happen

OpenStudy (kainui):

I was bored and thought I'd help. But I only rewrote your program so that it looked cleaner. English to Morse Code works, but not the other way around. I can help you only if you ask good questions. I left some stuff commented out in the code. Try to look at what happens when you uncomment the code to see what the program is doing at particular times. Those are actual things I used to help me trouble shoot the program as I was making it. ``` import java.util.Scanner; public class MorseCodeTranslator { static String[] Morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", "-----", "-....", "--...", "---..", "----.", "-----", "|" }; static String[] English = { "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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " " }; public StringBuilder sb; public static void main(String[] args) { MorseCodeTranslator mct = new MorseCodeTranslator(); mct.sb = new StringBuilder(); Scanner s = new Scanner(System.in); System.out.println("Type your message in English or Morse Code."); String text = s.nextLine(); if (text.startsWith(".") || text.startsWith("-")) { text = mct.morseToEnglish(text); } else { text = mct.englishToMorse(text); } System.out.println("Translated message:"); System.out.println(text); s.close(); } private String englishToMorse(String text) { String[] letters = text.split(""); for (int i = 0; i < letters.length; i++) { for (int j = 0; j < English.length; j++) { // System.out.println(letters[i] + " " + English[j]); if (letters[i].equals(English[j])) { sb.append(Morse[j] + " "); // System.out.println(sb.toString()); continue; } } } return sb.toString(); } private String morseToEnglish(String text) { // Figure it out, I'll help only if you ask good questions. return text; } } ``` The only part of this code that needs to be finished is the final method morseToEnglish. It is very similar to the englishToMorse method by design so that you can do similar things to help you understand. I will answer questions about the individual functionality of lines of code only.

OpenStudy (anonymous):

One thing to consider doing is an `indexOf` method for finding stuff in arrays. ``` public int indexOf(Object[] array, Object item){ for (int i = 0; i < array.length; i++) { if (item.equals(array[i])) { return i; } } return -1; } ```

OpenStudy (anonymous):

Not sure if java uses `.lenth()` or just `.length`. My guess is `.length()`.

OpenStudy (davidusa):

hey! im back.

OpenStudy (davidusa):

wait let me get caught up on this, i was at dinner

OpenStudy (kainui):

@wio Thanks, in Java I believe .length for arrays but .length() for array lists.

OpenStudy (davidusa):

@Kainui thanks i tested it it does the same thing my program does. do u want to use twiddla to comunicate or another website or stay here?

OpenStudy (kainui):

This site is fine, I am not going to be coming and going since I'm busy doing other things right now.

OpenStudy (davidusa):

are you using notepad with windows command prompt or are you using netbeans or java eclipse? i am using notepad with command prompt to compile and run my java files

OpenStudy (davidusa):

@ganeshie8 HELP ME JAVA PROGRAMMING

OpenStudy (kainui):

I use eclipse for my IDE, but I've used notepad++ a little as well. I've made a few programs with netbeans since that's the IDE they have at the library computers at my university, but I didn't get too much into it. I just got eclipse because that's generally what people use who are developing apps for android in Java and I just started teaching myself Java this past winter.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!