Reputation: 79
I created a program that will translate the input to Morse code and show it as output. Here is my program.
The class where I translate the message.
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class MorseCode {
public MorseCode()
{
}//end default constructor
public String[] translateHere(String s)throws IOException
{
String compare = s, codedLine = ""; //userInput toUpperCase
int length = compare.length(); //length of userInput
String line, file = "morsecode.txt";// variable holding file name and variable for each letter/number
char code;
//Constants
final int MAX = 36;
//Arrays
char[] morseLetter = new char[MAX];
String[] morseCode = new String[MAX];
String[] newMessage = new String[length];
//putting user input in a character array;
char[] userLetters = compare.toCharArray();
//object creation
File openFile = new File(file);
Scanner inFile = new Scanner(openFile);
int counter = 0;
while(inFile.hasNext())
{
line = inFile.next();
code = (char)line.charAt(0);
//System.out.println(code);
morseLetter[counter] = code;
morseCode[counter] = inFile.next();
counter++;
}//end nested while loop
for(int j = 0; j < length; j++)
{
for(int k = 0; k < MAX; k++)
{
if(userLetters[j] == morseLetter[k])
{
newMessage[j] = morseCode[k];
}
}//end nested for loop
}//end for loop
return newMessage;
}//end method that completes translateion
public String toString(String a, String[] b)
{
System.out.println("Input: " + a);
System.out.println("Output:");
String output = "";
for(int i = 0; i < b.length; i++)
{
output = output + b[i];
}
return output;
}//end toString method
}//end Translate Class
Then this is where i tested it.
package morse.code;
import javax.swing.JOptionPane;
import java.io.*;
public class MorseCodeTester
{
public static void main(String[] args)throws IOException
{
String userInput;
final String SENTINEL = "0";//for exiting program when entered
//object creation
MorseCode text = new MorseCode();
//getting user input to be translated
do
{
userInput = JOptionPane.showInputDialog("Please enter what you wish to translte to Morse code (no punctuation).");
String compare = userInput.toUpperCase();
String[] codedText = new String[compare.length()];
codedText = text.translateHere(compare);
text.toString(userInput, codedText);
}while(!userInput.equals(SENTINEL));
}//end main
}//end class
Everything seems fine here but right after i enter my input it shows this error.
Exception in thread "main" java.io.FileNotFoundException: morsecode.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:656)
at morse.code.MorseCode.translateHere(MorseCode.java:30)
at morse.code.MorseCodeTester.main(MorseCodeTester.java:23)
Java Result: 1
Upvotes: 0
Views: 1896
Reputation: 29
I have a morse code converter if you want it. It makes dots as - and dashes as --- but you could change that if you want.
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Enter Text For Conversion to Morse Code");
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
System.out.println(print(toMorse(s)));
}
static boolean[] toMorse(String input) {
boolean output[] = new boolean[0];
for (int i = 0; i < input.length(); i++) { // Every character
char onChar = input.charAt(i);
output = addCharacter(output, onChar);
}
return output;
}
static boolean[] addCharacter(boolean[] input, char character) {
character = Character.toLowerCase(character);
if (character == 'a') {
input = dot(input);
input = dash(input);
}
if (character == 'b') {
input = dash(input);
input = dot(input);
input = dot(input);
input = dot(input);
}
if (character == 'c') {
input = dash(input);
input = dot(input);
input = dash(input);
input = dot(input);
}
if (character == 'd') {
input = dash(input);
input = dot(input);
input = dot(input);
}
if (character == 'e') {
input = dot(input);
}
if (character == 'f') {
input = dot(input);
input = dot(input);
input = dash(input);
input = dot(input);
}
if (character == 'g') {
input = dash(input);
input = dash(input);
input = dot(input);
}
if (character == 'h') {
input = dot(input);
input = dot(input);
input = dot(input);
input = dot(input);
}
if (character == 'i') {
input = dot(input);
input = dot(input);
}
if (character == 'j') {
input = dot(input);
input = dash(input);
input = dash(input);
input = dash(input);
}
if (character == 'k') {
input = dash(input);
input = dot(input);
input = dash(input);
}
if (character == 'l') {
input = dot(input);
input = dash(input);
input = dot(input);
input = dot(input);
}
if (character == 'm') {
input = dash(input);
input = dash(input);
}
if (character == 'n') {
input = dash(input);
input = dot(input);
}
if (character == 'o') {
input = dash(input);
input = dash(input);
input = dash(input);
}
if (character == 'p') {
input = dot(input);
input = dash(input);
input = dash(input);
input = dot(input);
}
if (character == 'q') {
input = dash(input);
input = dash(input);
input = dot(input);
input = dash(input);
}
if (character == 'r') {
input = dot(input);
input = dash(input);
input = dot(input);
}
if (character == 's') {
input = dot(input);
input = dot(input);
input = dot(input);
}
if (character == 't') {
input = dash(input);
}
if (character == 'u') {
input = dot(input);
input = dot(input);
input = dash(input);
}
if (character == 'v') {
input = dot(input);
input = dot(input);
input = dot(input);
input = dash(input);
}
if (character == 'w') {
input = dot(input);
input = dash(input);
input = dash(input);
}
if (character == 'x') {
input = dash(input);
input = dot(input);
input = dot(input);
input = dash(input);
}
if (character == 'y') {
input = dash(input);
input = dot(input);
input = dash(input);
input = dash(input);
}
if (character == 'z') {
input = dash(input);
input = dash(input);
input = dot(input);
input = dot(input);
}
if (character == ' ') {
input = add(input, false);
input = add(input, false);
input = add(input, false);
input = add(input, false);
}
if (character == '1') {
input = dot(input);
input = dash(input);
input = dash(input);
input = dash(input);
input = dash(input);
}
if (character == '2') {
input = dot(input);
input = dot(input);
input = dash(input);
input = dash(input);
input = dash(input);
}
if (character == '3') {
input = dot(input);
input = dot(input);
input = dot(input);
input = dash(input);
input = dash(input);
}
if (character == '4') {
input = dot(input);
input = dot(input);
input = dot(input);
input = dot(input);
input = dash(input);
}
if (character == '5') {
input = dot(input);
input = dot(input);
input = dot(input);
input = dot(input);
input = dot(input);
}
if (character == '6') {
input = dash(input);
input = dot(input);
input = dot(input);
input = dot(input);
input = dot(input);
}
if (character == '7') {
input = dash(input);
input = dash(input);
input = dot(input);
input = dot(input);
input = dot(input);
}
if (character == '8') {
input = dash(input);
input = dash(input);
input = dash(input);
input = dot(input);
input = dot(input);
}
if (character == '9') {
input = dash(input);
input = dash(input);
input = dash(input);
input = dash(input);
input = dot(input);
}
if (character == '0') {
input = dash(input);
input = dash(input);
input = dash(input);
input = dash(input);
input = dash(input);
}
return input;
}
static boolean[] dot(boolean[] input) {
input = add(input, true);
input = add(input, false);
return input;
}
static boolean[] dash(boolean[] input) {
input = add(input, true);
input = add(input, true);
input = add(input, true);
input = add(input, false);
return input;
}
static boolean[] add(boolean[] input, boolean add) {
int length = input.length;
boolean[] newArray = new boolean[length + 1];
for (int i = 0; i < length; i++) {
newArray[i] = input[i];
}
newArray[length] = add;
return newArray;
}
static String print(boolean[] input) {
String output = "";
for (int i = 0; i < input.length; i++) {
if (input[i]) {
output = output + "-";
}
if (!input[i]) {
output = output + " ";
}
}
return output;
}
}
Upvotes: 0
Reputation: 27346
If you read the documentation you would know that this exception is thrown when a file can not be found by the JVM. I suspect that you need to update String line, file = "morsecode.txt"; //
to accurately reflect the location of the morsecode.txt
file.
Upvotes: 3