Simple code.. why isn't it working?! (no one has been able to solve this for me yet!)
My code has to read morse code from a text file, ask a user for a sentence, then translate the user's input into morse code. This is what I've written:
Code :
import java.util.*;
import java.io.*;
public class MorseCode
{
public static void main(String[] args) throws Exception
{
Scanner inFile = new Scanner(new File("morsecode.txt"));
Scanner in = new Scanner(System.in);
System.out.print("Please enter a sentence to be translated to morse code: ");
String userInput = in.nextLine();
String replacee[] = new String[36];
String replacer[] = new String[36];
String newInput[] = new String[36];
for(int i = 0 ; i < 36 ; i++)
{
replacee[i] = inFile.next();
replacer[i] = inFile.nextLine().replace(" ", "*");
newInput[i] = userInput.replace(replacer[i], replacee[i]);
}
System.out.println(newInput);
}
}
In case you're wondering, my text file looks something like this:
Code :
A .-
B -...
C -.-.
D -..
E .
F ..-.
G --.
H ....
I ..
[continuing with all of the letters]
0 -----
1 .----
2 ..---
3 ...--
4 ....-
5 .....
6 -....
7 --...
8 ---..
9 ----.
Re: Simple code.. why isn't it working?! (no one has been able to solve this for me y
You don't tell us what the program actually does wrong. And since we don't have access to that file, we can't really run it to find out. If you want help with code, you should provide an SSCCE that demonstrates the problem.
Re: Simple code.. why isn't it working?! (no one has been able to solve this for me y
I believe you should start by reading the whole text into and array. And it might be easier to use ascii or unicode instead of the actual letters. i.e 65 = A; 66 = b so the text file would be
Code :
//0-9
48 -----
ect...
57 ----.
//a-z
65 .-
66 -...
ect...
this way string morseCode[index] would hold the morse codes, i.e. morseCode[65] = ".-", morceCode[66] = "-..."
then all you need to do is: userCode = (int) userInput; System.out.println(morseCode[userCode]);
You could even eliminate the numbers in the text file, just remember to list the morse for numbers first and the list the codes for letters last.
of-corse when looping through the text file you would need to do two loops, one for numbers, using indexCode = i + 47, and the other loop for letters, using indexCode = i + 64. This is made use of by doing the folowing: morseCode[indexCode] = (next string from file)
This may not be the best way, i'm new to java myself. However this is the way I would have done it in Qbasic. I mean, you cannot use "a" or "b" or "c" as in morseCode[a], morseCode[b] since those would be considered variables and all have the value of 0. And coding it to read the character first and then the code is just a headache, just read the morse code into an array and forget the rest. Also this reduces the disk access. Once you've read it all in you don't need the text file any more. The data could be hardcoded, but I'm assuming reading the text file is part of the assignment.
note: ascii(48-57) is 0-9 and ascii(65-90) is a-z.
Re: Simple code.. why isn't it working?! (no one has been able to solve this for me y
I disagree with that advice, but it doesn't really matter as this question is six months old and the OP is most likely long gone. Is there a reason you're resurrecting old threads?
Re: Simple code.. why isn't it working?! (no one has been able to solve this for me y
sorry was bored did not look at the date