Commit aafe3ea0 authored by neil.whitehead's avatar neil.whitehead

12:30 03/01/19

parent 1cbc0815
......@@ -3,17 +3,12 @@ import java.util.List;
import java.io.*;
public class AssessmentPartFour
{
{
List<String> morseCode = new ArrayList<String>();
//COMMENT THIS A LOT BETTER
public int loadMorseFromFile(String filename)
{
......@@ -43,57 +38,51 @@ public class AssessmentPartFour
//MOVE THE .GET INTO IT'S OWN VARIABLE.
public String translateMorse()
{
for(int i = 0; i < morseCode.size(); i++)
{
System.out.println(morseCode.get(i) + " " + (morseCode.get(i)).length());
}
String strTranslated = "strTranslated ";
String strTranslated = "";
String[][] strMorseArray =
{{" ", "E", "T"}, // have 1 char
{"/", ".", "-"},
{"A", "I", "N", "M"}, // have 2 char
{".-", "..", "-.", "--"},
{"D", "G", "K", "O", "R", "S", "U", "W"}, // have 3 char
{"-..", "--.", "-.-", "---", ".-.", "...", "..-", ".--"},
{"B", "C", "F", "H", "J", "L", "P", "Q", "V", "X", "Y", "Z"}, // have 4 char
{"-...", "-.-.", "..-.", "....", ".---", ".-..", ".--.", "--.-", "..._", "-..-", "-.--", "--.."},
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}, // have 5 char
{".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"}};
//morse-letter lookup table, ordered by morse code length
/* 0 */ {{" ", "E", "T"}, // have 1 char
/* 1 */ {"/", ".", "-"},
/* 2 */ {"A", "I", "N", "M"}, // have 2 char
/* 3 */ {".-", "..", "-.", "--"},
/* 4 */ {"D", "G", "K", "O", "R", "S", "U", "W"}, // have 3 char
/* 5 */ {"-..", "--.", "-.-", "---", ".-.", "...", "..-", ".--"},
/* 6 */ {"B", "C", "F", "H", "J", "L", "P", "Q", "V", "X", "Y", "Z"}, // have 4 char
/* 7 */ {"-...", "-.-.", "..-.", "....", ".---", ".-..", ".--.", "--.-", "...-", "-..-", "-.--", "--.."},
/* 8 */ {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}, // have 5 char
/* 9 */ {".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"}};
for(int i = 0; i < morseCode.size(); i++)
//loops through each morse word within list (which has been taken from file)
{
String strMorseLetter;
int x = (((morseCode.get(i)).length()) * 2) - 1;
//length of the morse input, times 2, minus 1 is the x co-ordinate of morse code within the array.
for(int y = 0; y < strMorseArray[x].length; y++)
{
System.out.println("something");
System.out.println(strMorseArray[x-1][y]);
//loop through to identify y co-ordinate
{
if(strMorseArray[x][y].contains(morseCode.get(i)))
{
strTranslated.concat(strMorseArray[x-1][y]);
strMorseArray[x-1][y].concat(strTranslated);
//contains runs fractionally faster than equals.
//only checks against morse code which is the same length, so ".." containing "." is not an issue.
{
strTranslated = strTranslated.concat(strMorseArray[x-1][y]);
//x minus one = corresponding translation of morse code within the array
//adding recently matched letter to translated string
break;
}
}
}
}
//NEED TO SORT OUT LOOPS. WHY HAVE I DIVIDED THE MORSE ARRAY LIKE THIS AGAIN? WHY AREN'T MY STRINGS CONCATNATING?
System.out.println(strTranslated);
return "";
strTranslated = strTranslated.toLowerCase();
//converting to lowercase to comply with unit test
System.out.println("strTranslated = " + strTranslated);
return strTranslated;
}
}
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