Commit 6d8ed013 authored by liam.wadsworth's avatar liam.wadsworth

final commit attempt 2

parent 0225d2fd
Pipeline #468 failed with stages
......@@ -10,35 +10,35 @@ public class AssessmentPartFour {
List<String> morseCode = new ArrayList<String>();
public int loadMorseFromFile(String filename)
{Scanner scan;
morseCode.clear();
File file = new File(filename);
try {
scan = new Scanner(file);
{Scanner scan; //Defines the scanner that will be used for reading the file
morseCode.clear(); //Clears the variable that stores the file so that if the program is rerun, it doesn't keep the old file
File file = new File(filename); //Gets the file that will be translated and stores it in a variable
try { //This try is used for handling if there is an error reading the file
scan = new Scanner(file); //Reads the file
}
catch(FileNotFoundException e)
catch(FileNotFoundException e) //A condition that occurs if the file isn't found
{
System.out.println("Error reading file, exiting program");
return -1;
}
String line;
String line; //Will be used for storing an individual line from the file
while(scan.hasNextLine())
{
line = scan.nextLine();
morseCode.add(line);
morseCode.add(line); //Adds the line to an arraylist
}
return morseCode.size();
return morseCode.size(); //Returns the length of the arraylist
}
public String translateMorse()
{
String translatedCode = "";
for(int i = 0; i < morseCode.size(); i++)
String translatedCode = ""; //A string that will be used to hold the translated message
for(int i = 0; i < morseCode.size(); i++)//Loops once for every line in the code
{
char translatedChar = ' ';
switch(morseCode.get(i))
char translatedChar = ' '; //An individual character that will be translated
switch(morseCode.get(i)) //A switch statement that compares the line to every letter in the alphabet
//in morse code and assigns that letter to a variable
{
case".-": translatedChar = 'a';
break;
......@@ -93,7 +93,7 @@ public class AssessmentPartFour {
case"--..": translatedChar = 'z';
break;
}
translatedCode = translatedCode + translatedChar;
translatedCode = translatedCode + translatedChar; //Adds the translated character to the final message string
}
return translatedCode;
}
......
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