JAVA PROGRAMMING HELP
thanks im gonna post question now oops lol
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.
Do you know Java programming?
show me code
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
how are your parsing your input
@aajugdar java programming
heres the file
thanks for coming @aajugdar and @dan815
@dark_riddles we need your professional help @ganeshie8 These guys program for a living
Okay David some qusetions for you
I need to redownload netbeans right now to open java files again
NO. get jdk
jdk much better @ganeshie8 thanks for coming 2 help
tell me what you are basically doing for going from morse to english
You have | separting words
yes...
How do you know where the letter is ending
there are spaces between letters and | between words
okay
so it would be like m y | n a m e | i s | d a v i d
and morse with be like -.-- Space .., space....- | .... right?
i think...
okay well you can split for every | in the input
then split for ever space
and do a loop up for each string now
yay i reported aadjugar because his post about whatever, and a mod deleted it yay
hm im not sure what ur sayin
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
are you also goin to put in sound for the morse code?
It would be cool if you made a program that was interpreting the sound and sending the morse code sounds
lemme see who else does java..
@Kainui @wio
``` 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] + " "); } } } } } ```
Do you know how functions work?
wio will own this question
yes sorry i was gone for a while making vid so u guys can see my problem
You can use `String[] parts = morse.split(' ');` to split a string `morse` up by the spaces.
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 ```
thanks here are the instructions
@dan815 no i wont make sounds lol sorry look http://screencast-o-matic.com/watch/c21qrrnCyj
can u here in my vid? im not sure because my sound is muted, but i wanna know
Aww that would have been fun :)
wow everyone is afk this is... :( :P
hold on, I'm looking at your code.
Ok thanks
@Dark_Riddles Please help java programming
ok, I got no idea how your code is supposed to work.
Why is my program not working? Is there a way to make it work without changing it a lot?
Lol
You should use: `String[] morseLetters = morse.split(' ');`
Then go through each letter, looking it up, and printing it out.
ok thanks
Sorry, can't use sound right now.
what would i delete and where would i put the string thing
That's the fun of programming. =P
you declared `word` as an array, but you're comparing it against a character
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. } ```
can u edit my program and send it back because im getting a little confused.
what I wrote should completely replace your 'toEnglish' function.
ok. from there to the bottom?
Yes
thanks let me try
except for the print statements
nope tell me what part of the code you don't get.
theres an error message....
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?
okay, how will you fill it out?
what?
`// try to find morseLetter in the morse reference here.` This part needs to be replaced with other code.
i have no idea.
GAAHHH @WIO HELP!
@iambatman help me
.... no you need to go through the morse reverence array, then check if the the entry equals the morse letter.
Did you write any of the code you had originally...?
finally hi dan
Yes? What can I do, i dont know Java syntax
wio has you covered
David... what is the problem? You are not explaining where you are stuck.
Did you come just to get an answer?
@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.
David, you aren't asking a single question. That is why you aren't learning.
wait what i was afk what happen
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.
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; } ```
Not sure if java uses `.lenth()` or just `.length`. My guess is `.length()`.
hey! im back.
wait let me get caught up on this, i was at dinner
@wio Thanks, in Java I believe .length for arrays but .length() for array lists.
@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?
This site is fine, I am not going to be coming and going since I'm busy doing other things right now.
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
@ganeshie8 HELP ME JAVA PROGRAMMING
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.
Join our real-time social learning platform and learn together with your friends!