Commit 1fcc2748 authored by Romain's avatar Romain

final update

parent f0f2855a
......@@ -7,29 +7,29 @@ import java.util.Scanner;
public class AssessmentPartFour {
List<String> morseCode = new ArrayList<String>();
List<String> morseCode = new ArrayList<String>();
public int loadMorseFromFile(String filename) {
int number=0;
morseCode.clear();
int number=0; // Create an int to store the number of line
morseCode.clear(); // just to be sure the Array list is clear before start the method
try {
Scanner scanner = new Scanner(new File(filename));
Scanner scanner = new Scanner(new File(filename)); // to open the file
try {
number=0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
while (scanner.hasNextLine()) { // to run each line of the file
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 {
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());
}
......@@ -41,24 +41,24 @@ public class AssessmentPartFour {
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));
return new String(to);
System.out.println(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 alphabet = 0;
char morse; // to store the final translation
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';
break;
case "-...": alphabet= 'b';
......@@ -131,11 +131,11 @@ public class AssessmentPartFour {
break;
case "-----": alphabet= '0';
break;
case "/": alphabet= ' ';
case "/": alphabet= ' ';
break;
}
morse=alphabet;
morse=alphabet; // just to store the letter in a final Char.
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