Commit 8fcf6172 authored by christopher.spray's avatar christopher.spray

Update AssessmentPartFour.java

parent c899adb3
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
public class AssessmentPartFour {
......@@ -7,14 +12,64 @@ public class AssessmentPartFour {
public int loadMorseFromFile(String filename)
{
morseCode.clear(); //Clear all contents of the ArrayList so it doesn't add them all together
return 0;
Path filepath = Paths.get(filename); //find the file
String fileLine; //create an empty string
Scanner sc = null; //create scanner
try {
sc = new Scanner(filepath); //attempt to read the file
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); //if an error is thrown, print Stack Trace
}
while(sc.hasNextLine()) { //while there is a next line
fileLine = sc.nextLine(); //fileLine is the current line being read
morseCode.add(fileLine); //add current line to ArrayList
}
return morseCode.size(); //return the size of the ArrayList morseCode
}
public String translateMorse()
{
HashMap<String,Character>translation = new HashMap<>(); //create a hashmap to store morse code and it's alphabetic equivelant
translation.put(".-",'a'); //add morse for a, and a
translation.put("-...",'b'); //rinse and repeat
translation.put("-.-.",'c');
translation.put("-..",'d');
translation.put(".",'e');
translation.put("..-.",'f');
translation.put("--.",'g');
translation.put("....",'h');
translation.put("..",'i');
translation.put(".---",'j');
translation.put("-.-",'k');
translation.put(".-..",'l');
translation.put("--",'m');
translation.put("-.",'n');
translation.put("---",'o');
translation.put(".--.",'p');
translation.put("--.-",'q');
translation.put(".-.",'r');
translation.put("...",'s');
translation.put("-",'t');
translation.put("..--",'u');
translation.put("...-",'v');
translation.put(".--",'w');
translation.put("-..-",'x');
translation.put("-.--",'y');
translation.put("--..",'z');
translation.put("/",' ');
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