String to Char in Java GUI

Having to create a Java GUI and convert a string into morse code.

Page 1 of 1

6 Replies - 4888 Views - Last Post: 04 November 2009 - 09:17 PM Rate Topic: -----

#1 dragonwolf71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 09

String to Char in Java GUI

Posted 04 November 2009 - 02:06 PM

I have the mass majority of my program set up and have played with it a little bit. My main question is:
Is there a ".toCharArray()" equivalent when working with doing java GUI? Or should I do it differently? I appreciate the help.

Here is the code I need help with:
I borrowed it from my phoneticAlphabet project. I'm going to be changing it to morse code when I get this part figured.

// Create a lebel that will display text in morse code
			coded = new JLabel(" ");
			
			char []stringArray;
			stringArray = words.toCharArray();

			for(int i = 0; i < stringArray.length; i++)
			{
				char ch = stringArray[i];

				switch(ch)
				{
					case 'a':
					case 'A': System.out.print("Alpha "); break;
					case 'b':
					case 'B': System.out.print("Bravo "); break;
					case 'c':
					case 'C': System.out.print("Charlie "); break;
					case 'd':
					case 'D': System.out.print("Delta "); break;
					case 'e':
					case 'E': System.out.print("Echo "); break;
					case 'f':
					case 'F': System.out.print("Foxtrot "); break;
					case 'g':
					case 'G': System.out.print("Golf "); break;
					case 'h':
					case 'H': System.out.print("Hotel "); break;
					case 'i':
					case 'I': System.out.print("India "); break;
					case 'j':
					case 'J': System.out.print("Juliet "); break;
					case 'k':
					case 'K': System.out.print("Kilo "); break;
					case 'l':
					case 'L': System.out.print("Lima "); break;
					case 'm':
					case 'M': System.out.print("Mike "); break;
					case 'n':
					case 'N': System.out.print("November "); break;
					case 'o':
					case 'O': System.out.print("Oscar "); break;
					case 'p':
					case 'P': System.out.print("Papa "); break;
					case 'q':
					case 'Q': System.out.print("Quebec "); break;
					case 'r':
					case 'R': System.out.print("Romeo "); break;
					case 's':
					case 'S': System.out.print("Sierra "); break;
					case 't':
					case 'T': System.out.print("Tango "); break;
					case 'u':
					case 'U': System.out.print("Uniform "); break;
					case 'v':
					case 'V': System.out.print("Victor "); break;
					case 'w':
					case 'W': System.out.print("Whiskey "); break;
					case 'x':
					case 'X': System.out.print("Xray "); break;
					case 'y':
					case 'Y': System.out.print("Yankee "); break;
					case 'z':
					case 'Z': System.out.print("Zebra "); break;

					default:
						System.out.print(ch + " ");
				}
			}
			System.out.println();
			
			contentPane.add(coded);


This post has been edited by dragonwolf71: 04 November 2009 - 05:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: String to Char in Java GUI

#2 Tanira  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 102
  • Joined: 30-May 09

Re: String to Char in Java GUI

Posted 04 November 2009 - 02:14 PM

View Postdragonwolf71, on 4 Nov, 2009 - 01:06 PM, said:

I have the mass majority of my program set up and have played with it a little bit. My main question is:
Is there a ".toCharArray()" equivalent when working with doing java GUI? Or should I do it differently? I appreciate the help.

Here is the code I need help with:
I borrowed it from my phoneticAlphabet project. I'm going to be changing it to morse code when I get this part figured.

// Create a lebel that will display text in morse code
			coded = new JLabel(" ");
			
			char []stringArray;
			stringArray = words.toCharArray();

			for(int i = 0; i < stringArray.length; i++)
			{
				char ch = stringArray[i];

				switch(ch)
				{
					case 'a':
					case 'A': System.out.print("Alpha "); break;
					case 'b':
					case 'B': System.out.print("Bravo "); break;
					case 'c':
					case 'C': System.out.print("Charlie "); break;
					case 'd':
					case 'D': System.out.print("Delta "); break;
					case 'e':
					case 'E': System.out.print("Echo "); break;
					case 'f':
					case 'F': System.out.print("Foxtrot "); break;
					case 'g':
					case 'G': System.out.print("Golf "); break;
					case 'h':
					case 'H': System.out.print("Hotel "); break;
					case 'i':
					case 'I': System.out.print("India "); break;
					case 'j':
					case 'J': System.out.print("Juliet "); break;
					case 'k':
					case 'K': System.out.print("Kilo "); break;
					case 'l':
					case 'L': System.out.print("Lima "); break;
					case 'm':
					case 'M': System.out.print("Mike "); break;
					case 'n':
					case 'N': System.out.print("November "); break;
					case 'o':
					case 'O': System.out.print("Oscar "); break;
					case 'p':
					case 'P': System.out.print("Papa "); break;
					case 'q':
					case 'Q': System.out.print("Quebec "); break;
					case 'r':
					case 'R': System.out.print("Romeo "); break;
					case 's':
					case 'S': System.out.print("Sierra "); break;
					case 't':
					case 'T': System.out.print("Tango "); break;
					case 'u':
					case 'U': System.out.print("Uniform "); break;
					case 'v':
					case 'V': System.out.print("Victor "); break;
					case 'w':
					case 'W': System.out.print("Whiskey "); break;
					case 'x':
					case 'X': System.out.print("Xray "); break;
					case 'y':
					case 'Y': System.out.print("Yankee "); break;
					case 'z':
					case 'Z': System.out.print("Zebra "); break;

					default:
						System.out.print(ch + " ");
				}
			}
			System.out.println();
			
			contentPane.add(coded);



First off in Java Strings are nothing more than char[]
So I don't see why you're referencing char[] when you can just use a String.
It's not a problem though so if you want to stick with char[] you can
it's just a pointer.
just use a toString() if a toCharArray doesnt exist.
Was This Post Helpful? 0
  • +
  • -

#3 dragonwolf71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 09

Re: String to Char in Java GUI

Posted 04 November 2009 - 04:40 PM

Well, the section of code that I posted is only a portion of the entire thing. The program has to accept a String and then separate it into characters. After doing that it has to turn the characters into its respective Morse code assignment.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: String to Char in Java GUI

Posted 04 November 2009 - 07:18 PM

@Tanira: there are a few mistakes/misconceptions in your affirmations :)

Dragonwolf

when you will write your GUI you will need something like a JTextField to get your user data
when the user will click on the button to translate the sentence you will have to

String word = textField.getText();
char[] stringArray = word.toCharArray();

and then you can continue with your actual algorithm
Was This Post Helpful? 0
  • +
  • -

#5 dragonwolf71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 09

Re: String to Char in Java GUI

Posted 04 November 2009 - 08:50 PM

This is my entire code at the moment. I did have the "char[] stringArray = words.toCharArray()" stuff in there but it doesn't like that. Not sure why. I'm getting rid of the switch statement and going to try to use an array. Any more help is great. Not asking for the entire answer. Just some more guidance. Thanks to all the help so far.

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);
		}

		public void actionPerformed(ActionEvent event)
		{
			String text = words.getText();
			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();
			}
		});
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: String to Char in Java GUI

Posted 04 November 2009 - 09:08 PM

View Postdragonwolf71, on 4 Nov, 2009 - 07:50 PM, said:

I'm getting rid of the switch statement and going to try to use an array.

Very good idea

String[] morse = {".-", "-...", "-.-/>.", 

if to change char digit;

digit = Character.toUpperCase(digit);	 // no need to check for 'a' and 'A'
int index = (int) digit;   // translate char into int
index -= (int) 'A';		// subtract 'A' so if 'A' index = 0 if 'Z' index will be 25
if(index >= 0 && index < morse.length)
	.... you can output morse[index]


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: String to Char in Java GUI

Posted 04 November 2009 - 09:17 PM

Ouf, you have a misconception of what a ContentPane is

Here much simpler

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

public class MorseCode extends JFrame implements ActionListener
{
	JTextField words;
	JButton CodeIt;
	JLabel coded;

	public MorseCode()
	{
		// Create and set up the frame
		super("Morse Code");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

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

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

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

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

		// Add content pane to frame
		add(p);

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

	public void actionPerformed(ActionEvent event)
	{
		String text = words.getText();
		char[] digit = text.toCharArray();
		////  ok you still have a bit to do
	}


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


Was This Post Helpful? 1

Page 1 of 1