//sets up new array lists that will be used by both methods
publicintloadMorseFromFile(Stringfilename)
publicintloadMorseFromFile(Stringfilename)
{
intlineCount=0;
try{
Filefile=newFile(filename);
Scannerscan=newScanner(file);
//if file is present it is loaded into the scanner
StringlineFromFile;
return0;
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(FileNotFoundExceptione){
// 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
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);
returnlineCount;
}
publicStringtranslateMorse()
{
return"";
intmorseSize=morseCode.size();
//uses array list with morse values stored from previous method
chartranslated='a';
char[]translationArray=newchar[morseSize];
//initializes variables and sets up char array for results
for(intindex=0;index<morseSize;index++)
//loop will only iterate through the size of the string to be translated
{
Stringvalue=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