This project is similar to, but not exactly like, my phonetic project. I am having to construct a GUI and have done so. I have been able to get everything except one part.

What I need it to do is:
1) User inputs a string (this part works)
2) When user clicks button "Code It" it needs to convert the string into Morse code. (this part is what isn't working)

I don't know how to set it up to do it. I'm trying to use an array to do it.

What it does:
1) Takes user's input
2) When user clicks button "Code It" it just displays the string they entered.

Any ideas and help will be appreciated.

package morsecode;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MorseCode
{
    public static class morseCode implements ActionListener
    {
        JFrame frame;
        JPanel contentPane;
        JTextField words;
        JButton CodeIt;
        JLabel enterString, coded;

        public morseCode()
        {
            // Create and set up the frame
            frame = new JFrame("Morse Code");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // Create a content pane with a BoxLayout
            // and empty borders
            contentPane = new JPanel();
            contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 100));
            contentPane.setLayout(new GridLayout(4,2,5,10));

            // Create a text field and a descriptive label
            enterString = new JLabel("Enter string: ");
            contentPane.add(enterString);

            words = new JTextField(50);
            contentPane.add(words);

            // Create a display morseCode button
            CodeIt = new JButton("Code It");
            CodeIt.addActionListener(this);
            contentPane.add(CodeIt);

            // Create a label that will display text in morse code
            coded = new JLabel(" ");
            contentPane.add(coded);

            // Add content pane to frame
            frame.setContentPane(contentPane);

            // Size and then diplay the frame
            frame.pack();
            frame.setVisible(true);
        }


//////////// I think this is the section where I need to put my array
//////////// set up since it retrieves the user input and I believe
//////////// displays it
        public void actionPerformed(ActionEvent event)
        {
            String text = words.getText();
//            char[] stringArray = text.toCharArray();
//            String[] morse = {".- ","-... ","-.-. ","-.. ",". ","..-. ","--. ",".... ",".. ",".--- ","-.- ",".-.. ","-- ","-. ","--- ",".--. ","--.- ",".-. ","... ","- ","..- ","...- ",".-- ","-..- ","-.-- ","--.. "};
            coded.setText(text);
        }
    }

    private static void runGUI()
    {
        JFrame.setDefaultLookAndFeelDecorated(true);

        morseCode greeting = new morseCode();
    }

    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                runGUI();
            }
        });
    }
}

What java version are you using?
If you are using 1.5 for example then you can map the letters with the morse code using a hashmap:

HashMap<Character,String> map = HashMap<Character,String>(26);

map.put('a', ".-");
map.put('b', "-...");
...


String text = words.getText().toLowerCase();
char[] stringArray = text.toCharArray();

Then you can loop through the array and use the char as the key to get the String value from the map.


String result="";
...
result += map.get(stringArray[i]);
...

Write new class

MorseEncoder

Place in it your array

String[] morse

Start from single char. Get result.
Later use array of char or String.
//
Alternatively you can do all work inside single static method.

I'm using NetBeans IDE 6.7.1
Not sure if that means much in the way of what version I'm using.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.