Search code examples
javamorse-code

Morsecode using static


I have this program but i have a few problems: This is the place where i do the calcs.

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 = "C:\\APCS  Course B\\Unit 4\\Unit 4 Documents\\morse.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

The main methods:

import javax.swing.JOptionPane;
import java.io.*;
import java.util.Scanner;
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
        {
            Scanner in = new Scanner(System.in);
            System.out.print("Please enter what you wish to translte to Morse code (no punctuation): ");
            userInput = in.nextLine();
            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

So my program is when I put in a input the output shows black even tho in the main methods i did the calculation that would convert it to Morse Code. Also is there any suggestions on if i could use the static identifier in any of my methods.

Here is the text file:

1 .----

2 ..---

3 ...--

4 ....-

5 .....

6 -....

7 --...

8 ---..

9 ----.

0 -----

A .-

B -...

C -.-.

D -..

E .

F ..-.

G --.

H ....

I ..

J .---

K -.-

L .-..

M --

N -.

O ---

P .--.

Q --.-

R .-.

S ...

T -

U ..-

V ...-

W .--

X -..-

Y -.--

Z --..

Solution

  • Your code should use a hashmap to key the morse code by letter, this makes your code much simpler. Getting the morsecode for a letter is then a simple lookup.

    Also you could just load the data once, maybe in the Constructor of MorseCode.

    But to get at your question, you are just not printing the output. Just returning it and not showing it on the console.

    if you change your toString method to look like this, it should print out something.

    public String toString(String a, String[] b) {
        System.out.println("Input: " + a);
        System.out.print("Output:"); // changed to a print, to avoid newline
        String output = "";
        for (int i = 0; i < b.length; i++) {
            output = output + b[i];
        }
        System.out.println(output); // this was missing
        return output;
    }//end toString method