Commit 1fcc2748 authored by Romain's avatar Romain

final update

parent f0f2855a
...@@ -11,25 +11,25 @@ public class AssessmentPartFour { ...@@ -11,25 +11,25 @@ public class AssessmentPartFour {
public int loadMorseFromFile(String filename) { public int loadMorseFromFile(String filename) {
int number=0; int number=0; // Create an int to store the number of line
morseCode.clear(); morseCode.clear(); // just to be sure the Array list is clear before start the method
try { try {
Scanner scanner = new Scanner(new File(filename)); Scanner scanner = new Scanner(new File(filename)); // to open the file
try { try {
number=0; number=0;
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) { // to run each line of the file
String line = scanner.nextLine(); String line = scanner.nextLine(); // create a string with the line of the file
morseCode.add(line); morseCode.add(line); // each line is added in the array list
number=morseCode.size(); number=morseCode.size(); // number return the length of the array list
} }
}finally { }finally {
scanner.close(); scanner.close(); // close the file
} }
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) { // in case if the file is not found
System.out.println("Erreur --" + ex.toString()); System.out.println("Erreur --" + ex.toString());
} }
...@@ -41,24 +41,24 @@ public class AssessmentPartFour { ...@@ -41,24 +41,24 @@ public class AssessmentPartFour {
public String translateMorse() public String translateMorse()
{ {
char [] to=new char[morseCode.size()]; char [] to=new char[morseCode.size()]; // create an array to store each character of the morse code
for(int i=0;i<morseCode.size();i++) for(int i=0;i<morseCode.size();i++) // to run each character in the array list
{ {
to[i]=morseToEnglish(morseCode.get(i)); to[i]=morseToEnglish(morseCode.get(i)); // the array take each morse character and translate it in english using a method
} }
System.out.println(new String(to)); System.out.println(new String(to));
return new String(to); return new String(to); // return and create a String from the array
} }
private static char morseToEnglish(String a) private static char morseToEnglish(String a) // create a method who translate each morse character in english
{ {
char morse; char morse; // to store the final translation
char alphabet = 0; char alphabet = 0; // to store the translation of morse code
switch(a) { switch(a) { // to assign each morse character to its letter
case ".-" : alphabet = 'a'; case ".-" : alphabet = 'a';
break; break;
case "-...": alphabet= 'b'; case "-...": alphabet= 'b';
...@@ -135,7 +135,7 @@ public class AssessmentPartFour { ...@@ -135,7 +135,7 @@ public class AssessmentPartFour {
break; break;
} }
morse=alphabet; morse=alphabet; // just to store the letter in a final Char.
return morse; return morse;
} }
......
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