Commit d6ee1508 authored by Emman's avatar Emman

commit

parent c899adb3
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner;
public class AssessmentPartFour { public class AssessmentPartFour {
List<String> morseCode = new ArrayList<String>(); List<String> morseCode = new ArrayList<String>();
public int loadMorseFromFile(String filename) private char morseCode(String str) {
{ switch (str) {
case ".-":
return 0; return 'a';
case "-...":
return 'b';
case "-.-.":
return 'c';
case "-..":
return 'd';
case ".":
return 'e';
case "..-.":
return 'f';
case "--.":
return 'g';
case "....":
return 'h';
case "..":
return 'i';
case ".---":
return 'j';
case "-.-":
return 'k';
case ".-..":
return 'l';
case "--":
return 'm';
case "-.":
return 'n';
case "---":
return 'o';
case ".--.":
return 'p';
case "--.-":
return 'q';
case ".-.":
return 'r';
case "...":
return 's';
case "-":
return 't';
case "..-":
return 'u';
case "...-":
return 'v';
case ".--":
return 'w';
case "-..-":
return 'x';
case "-.--":
return 'y';
// for space
case "--..":
return 'z';
}
return '\0';
} }
public String translateMorse() public int loadMorseFromFile(String filename) {
{ morseCode.clear();
return ""; Scanner sc;
int count = 0;
String s = "";
try {
sc = new Scanner(new File(filename));
while (sc.hasNext()) {
String str = sc.next();
if (!"/".equals(str)) {
char ch=morseCode(str);
if ('\0' != ch) {
s+=morseCode(str);
count++;
}
}else {
count++;
s+=" ";
}
} }
} catch (FileNotFoundException e) {
}
if (!s.trim().isEmpty()) {
morseCode.add(s);
}
return count;
}
public String translateMorse() {
return morseCode.get(0);
}
} }
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