Commit 3feac6bd authored by tomun's avatar tomun

part four

parents
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class AssessmentPartFour {
List<String> morseCode = new ArrayList<String>();
public int loadMorseFromFile(String filename) throws IOException
{
int numOfLetters = 0;
try {
FileReader fr = new FileReader(filename);
BufferedReader in = new BufferedReader(fr);
String str;
while((str = in.readLine()) != null)
{
numOfLetters += 1;
}
in.close();
return numOfLetters;
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("error");
return 0;
}
}
public String translateMorse(String filename, int count) throws IOException
{
String[][] morseAlphabet = {{".-", "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"},{"/"," "}};
String translated = "";
int pos = 0;
String morse = "";
try {
FileReader fr = new FileReader(filename);
BufferedReader in = new BufferedReader(fr);
for(int i = 0; i < loadMorseFromFile(filename); i++)
{
morse = in.readLine();
for(int j = 0; j < morseAlphabet.length; j++)
{
if(morse.equals(morseAlphabet[j][0]))
{
translated += morseAlphabet[j][1];
}
}
}
//System.out.println(translated);
in.close();
return translated;
}
catch (FileNotFoundException e) {
System.out.println("error");
return "";
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment