1

I am very new to java and have been having trouble with my morse code program. Please bear with me, but when entering text to be translated from morse code to english it just prints null in place of letters. Any help would be greatly appreciated.

import java.util.Scanner;

public class MorseCode
{
    public static void main(String[] args)
    {
        Scanner Input = new Scanner(System.in);

        System.out.println("To convert English to Morse Code, type M. To convert Morse Code to English, type E.");

        String cType = Input.nextLine();

        String type = cType.toLowerCase();

        if("m".equals(type))
        {
            String eng;
            System.out.println("Please enter the English text to be translated.");
            eng = Input.nextLine();
            EToM(eng);
        }
        else
        {
            String morse;
            System.out.println("Please enter the Morse code text to be translated, with multiple words seperated by a |.");
            morse = Input.nextLine();
            MToE(morse);
        }
    }
    public static void EToM(String eng)
    {
        String EToMList[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".--", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "----.", "-----", "|"};
        String alphabet = "abcdefghijklmnopqrstuvwxyz123456789 ";
        String translation[] = new String[eng.length()];

        for(int x = 0; x < eng.length(); x++)
        {
            for(int y = 0; y < alphabet.length(); y++)
            {
                if(eng.charAt(x) == alphabet.charAt(y))
                {
                    translation[x] = EToMList[y];
                }
            }
        }

        System.out.println("Your translated message is:");

        for(int z = 0; z < eng.length(); z++)
        {
            System.out.println(translation[z]); 
        }
    }

    public static void MToE(String morse)
    {
        int arraySize = 0;
        String MToEList[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".--", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "----.", "-----", "|"};
        String alphabet = "abcdefghijklmnopqrstuvwxyz123456789 ";
        char space = '|';

        for(int x = 0; x < morse.length(); x++)
        {
            if(morse.charAt(x) == space)
            {
                arraySize += 1;
            }
        }

        String segmentedMessage[] = new String[arraySize];
        String translation[] = new String[arraySize];

        int a = 1;
        int counter = 0;

        for(int y = 0; y < morse.length(); y++)
        {
            if(morse.charAt(y) == space)
            {
                segmentedMessage[counter] = morse.substring((a - 1), (y + 1));
                a = y;
            }
        }

        for(int z = 0; z < segmentedMessage.length; z++)
        {
            for(int i = 0; i < alphabet.length(); i++)
            {
                if(segmentedMessage[z] == MToEList[i])
                {
                    translation[z] = alphabet.substring(i - 1, i + 1);
                }
            }
        }

        System.out.println("Your translated message is:");

        for(int j = 0; j < translation.length; j++)
        {
            System.out.println(translation[j]);
        }
    }
}       
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • 1
    .. .----. -- ... --- .-. .-. -.-- --..-- -... ..- - .. -.-. .- -. .----. - .-. . .- -.. -- --- .-. ... . -.-. --- -.. . .-.-.- – splungebob Aug 16 '13 at 18:19
  • Well, either break down your program into small chunks that you can test separately or use a debugger to see what's going on. – Kayaman Aug 16 '13 at 18:20

2 Answers2

1

I think your problem lies at the line

if(segmentedMessage[z] == MToEList[i])

See earlier you were comparing char types, char is a primitive type meaning you can use == to safely compare values. On the line I mentioned you are comparing String types. String is a java Object.

All Java objects should use .compare instead of ==

Example

char char1 = 'c';
char char2 = 'c';
char1 == char2 // true!

String str1 = "c";
String str2 = "c";
str1 == str2 // not always going to be true, even tho it would seem that way
str1.equals(str2) // true!
ug_
  • 11,267
  • 2
  • 35
  • 52
1

The first problem that I see is that in your MToE method, in this section:

int a = 1;
int counter = 0

for(int y = 0; y < morse.length(); y++)
{
    if(morse.charAt(y) == space)
    {
        segmentedMessage[counter] = morse.substring((a - 1), (y + 1));
        a = y;
    }
}

You never increment counter, and so only the first element of segmentedMessage will ever be filled with a morse string. If you're getting one character and then a bunch of nulls, this is probably your issue.

Further, in this line in the same method:

translation[z] = alphabet.substring(i - 1, i + 1);

You are taking the two element substring consisting of the i - 1th and the ith characters of alphabet. It looks like what you meant to do there is alphabet.substring(i, i + 1). In particular, this will cause issues if i = 0, since you will then be trying to take a substring starting at -1.

qaphla
  • 4,707
  • 3
  • 20
  • 31
  • I am still receiving a return of just null. I have no idea what is causing it. Do you have any other ideas? – Alexander Gulea Aug 16 '13 at 18:40
  • 1
    Have you also tried taking the advice of the other answer -- that is, using .equals to compare Strings, rather than ==? – qaphla Aug 16 '13 at 18:44
  • 1
    What you should do is debug your program. Take each bit of your program and make sure that your code is working how it should. For instance, in your MToE method you should put a println in your first loop when setting segmentedMessage[counter] and make sure that the value your setting it to is what its suppose to be (I just debugged it and its not). Continue checking each step of the program ensuring that your variables are actually set to what you KNOW they should be. This is known as the debugging process. – ug_ Aug 16 '13 at 18:58
  • 1
    I wanna help you out without solving your entire problem for you, debugging is a very important thing to learn in programming. But you should really use the String.split method, look at http://stackoverflow.com/questions/3481828/string-split-in-java Its very applicable for your program. Use in your case morse.split("\\|"); Wanted to include this so you dont have to learn regular expressions for your split. – ug_ Aug 16 '13 at 19:07