You are on page 1of 2

import java.io.

*;
import java.util.*;
import javax.swing.*;
/****************************************************************************
* @author Marcus Grant
* @version 16-2-2016
* MORSE CLASS that: 1) Reads in a text file with morse code on it.
* 2) Ask a user to enter sentences, that will be translated into morse
* code. 3) When the user hits the cancel button, a message will appear
* that shows what the user entered and the translation into morse code.
****************************************************************************/
public class Morse {
public static void main(String[] args) {
// ArrayList holds Morse Code characters
ArrayList<String> morseCode = new ArrayList<String>();
String userInput = "";
String option;

// User entered sentence


// Option to continue or not

// Try/Catch block for storing morse code and error handling


try {
// Variable scans thru and holds morse code file
Scanner scan = new Scanner(new File("C:/Morse.txt"));
int index = 0;
// Loop through Morse Code file
// and store values in ArrayList
while (scan.hasNext()) {
morseCode.add(scan.nextLine());
index++;
}
scan.close();
} catch(FileNotFoundException ex) {
// Prints error if file not found
System.out.println("Error reading file 'C:/Morse.txt'");
}
// Gives user instructions in dialog box
JOptionPane.showMessageDialog(null, "Enter a sentence to be
traslated into Morse Code\nHit cancel to quit");
do {
// User enters sentence and continues if 'Okay' is hit
option = JOptionPane.showInputDialog(null, "Enter a sent
ence here");
if (option != null) {
// Stores multiple user entered sentences
userInput += option + " ";
}

// Exit loop if 'Cancel is hit


} while (option != null);
// New Code Object with morse code and user input entered
Code translation = new Code(morseCode, userInput);
// Variable holds the translated English into morse code
ArrayList<String> showTran = new ArrayList<String>();
// Grabs the translation
showTran = translation.english2Morse();
// Show the original English sentence and translation into morse
JOptionPane.showMessageDialog(null, userInput + "\n" +showTran);
// Exit
System.exit(0);
}
}

You might also like