Commit e706f6f8 authored by william.hill1's avatar william.hill1

Update AssessmentPartFour.java

parent c899adb3
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class AssessmentPartFour { public class AssessmentPartFour {
List<String> morseCode = new ArrayList<String>(); List<String> morseCode = new ArrayList<String>();
public int loadMorseFromFile(String filename) public int loadMorseFromFile(String filename) {
{ File x = new File(filename); //sets the filename to be the file
BufferedReader buffer = null; //internalises the variable
return 0; int lines = 0; //internalises the line count
String Morse = null; //internalises the string
try { //used to catch exeptions
buffer = new BufferedReader(new FileReader(x)); // allows to read the file
while ((Morse = buffer.readLine()) != null) { //repeats this while there is a line available
morseCode.add(Morse); //adds the line into the arraylist
lines++; //increases the line count to the amount of lines in the file
}
} catch (IOException e) { //catches exceptions
e.printStackTrace(); //prints the trace to help debug
} finally {
try {
if (buffer != null) { //if the buffer isnt null
buffer.close(); // closes the buffer at the end
}
} catch (FileNotFoundException e) { //catches exceptions
e.printStackTrace(); //prints the trace
} catch (IOException e) { //catches exceptions
// TODO Auto-generated catch block
e.printStackTrace(); //prints the trace
}
}
System.out.println(morseCode); //prints the morsecode array to help me see if it all worked
return lines; //returns the line count
} }
public String translateMorse() public String translateMorse() {
{ String[] code = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", // the alphebet in morse including space "/"
return ""; "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "/" };
String[] Alphabet = { "a", " b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", // the alphebet in english
"s", "t", "u", "v", "w", "x", "y", "z", " " };
String Converted = "";
for (int i = 0; i < morseCode.size(); i++) { //loops the legnth of the list
for (int j = 0; j < code.length; j++) { //a nested loop looping the legnth of the morse code array
if (morseCode.get(i).equals(code[j])) { //if the arraylist element equals a code array element
Converted += Alphabet[j]; //converted = the position of the code array element in the Alphebet array
}
}
}
morseCode.clear(); //clears previous so it dosent convert the other bit too so if 1 was a and 2 was b it wouldnt return ab it would return b
return Converted;
} }
} }
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