Commit 3272fd0c authored by Thundy's avatar Thundy

It's finished

parent c899adb3
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class AssessmentPartFour { public class AssessmentPartFour {
List<Integer> lineList = new ArrayList<Integer>();
List<String> morseCode = new ArrayList<String>(); List<String> morseCode = new ArrayList<String>();
//sets up new array lists that will be used by both methods
public int loadMorseFromFile(String filename) public int loadMorseFromFile(String filename)
{ {
int lineCount=0;
try {
File file = new File(filename);
Scanner scan= new Scanner(file);
//if file is present it is loaded into the scanner
String lineFromFile;
while (scan.hasNextLine())
{
lineFromFile=scan.nextLine();
morseCode.add(lineFromFile);
lineCount++;
// iterates through each line of the file and stores a running total in lineCount
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
// if this happens, panic
e.printStackTrace();
}
// attempts to load requested file into the scanner, there is a try-catch included in order prevent errors, but lacks code since all files are present and this type of error is not expected
return 0;
lineList.add(lineCount);
//adds the value of lineCount to the LineList array list for future use
System.out.println("File name: "+filename+" No. of lines: "+lineCount);
return lineCount;
} }
public String translateMorse() public String translateMorse()
{ {
return ""; int morseSize=morseCode.size();
//uses array list with morse values stored from previous method
char translated='a';
char[] translationArray= new char[morseSize];
//initializes variables and sets up char array for results
for (int index=0;index<morseSize;index++)
//loop will only iterate through the size of the string to be translated
{
String value=morseCode.get(0);
switch(value)
{
case ".-":translated='a';break;
case "-...":translated='b';break;
case "-.-.":translated='c';break;
case "-..":translated='d';break;
case ".":translated='e';break;
case "..-.":translated='f';break;
case "--.":translated='g';break;
case "....":translated='h';break;
case "..":translated='i';break;
case ".---":translated='j';break;
case "-.-":translated='k';break;
case ".-..":translated='l';break;
case "--":translated='m';break;
case "-.":translated='n';break;
case "---":translated='o';break;
case ".--.":translated='p';break;
case "--.-":translated='q';break;
case ".-.":translated='r';break;
case "...":translated='s';break;
case "-":translated='t';break;
case "..-":translated='u';break;
case "...-":translated='v';break;
case ".--":translated='w';break;
case "-..-":translated='x';break;
case "-.--":translated='y';break;
case "--..":translated='z';break;
case "/":translated=' ';break;
}
//checks all morse code characters to find correct plain text character
translationArray[index]=translated;
morseCode.remove(0);
//inputs result at same position in character array
} }
String output=new String(translationArray);
//converts char array to string
System.out.println("Translation: "+output);
return output;
}
}
}
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