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 { ...@@ -10,35 +10,35 @@ 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)
{Scanner scan; {Scanner scan; //Defines the scanner that will be used for reading the file
morseCode.clear(); 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); 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
try { scan = new Scanner(file); //Reads the file
scan = new Scanner(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"); System.out.println("Error reading file, exiting program");
return -1; return -1;
} }
String line; String line; //Will be used for storing an individual line from the file
while(scan.hasNextLine()) while(scan.hasNextLine())
{ {
line = scan.nextLine(); 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() public String translateMorse()
{ {
String translatedCode = ""; String translatedCode = ""; //A string that will be used to hold the translated message
for(int i = 0; i < morseCode.size(); i++) for(int i = 0; i < morseCode.size(); i++)//Loops once for every line in the code
{ {
char translatedChar = ' '; char translatedChar = ' '; //An individual character that will be translated
switch(morseCode.get(i)) 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'; case".-": translatedChar = 'a';
break; break;
...@@ -93,7 +93,7 @@ public class AssessmentPartFour { ...@@ -93,7 +93,7 @@ public class AssessmentPartFour {
case"--..": translatedChar = 'z'; case"--..": translatedChar = 'z';
break; break;
} }
translatedCode = translatedCode + translatedChar; translatedCode = translatedCode + translatedChar; //Adds the translated character to the final message string
} }
return translatedCode; 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