i have a error in this program...i m tryin to write a java GUI program that converts character to corresponding integer and vice versa...tis is my code...can anyone help me solve the error???the error is :

1 error found:
File: C:\Users\Kumaresen Nair\Desktop\Conversion.java [line: 58]
Error: The local variable word may not have been initialized

this is my program code

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



public class Conversion extends JFrame
{
    private JLabel wordLabel;
    private JLabel numberLabel;

    private JTextField wordTF;
    private JTextField numberTF;

    private WordHandler wordHandler;
    private NumHandler numberHandler;

    private static final int WIDTH = 500;
    private static final int HEIGHT = 50;

    public Conversion()
    {
        setTitle("Word and Number Conversion");
        Container c = getContentPane();
        c.setLayout(new GridLayout(1, 4));

        wordLabel = new JLabel("String : ",
                                  SwingConstants.RIGHT);

        numberLabel = new JLabel("Number : ",
                                     SwingConstants.RIGHT);

        wordTF = new JTextField(7);
        numberTF = new JTextField(7);

        c.add(wordLabel);
        c.add(wordTF);
        c.add(numberLabel);
        c.add(numberTF);

        wordHandler = new WordHandler();
        numberHandler = new NumHandler();

        wordTF.addActionListener(wordHandler);
        numberTF.addActionListener(numberHandler);

        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    private class WordHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
           String word;
            int number;
             number = Integer.parseInt(word);
            numberTF.setText(" " +number);

        }
    }

    private class NumHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
          int number = 110;
String word;
word = Integer.toString(number);

  wordTF.setText(" " +word); 

        }
    }

    public static void main(String[] args)
    {

    }
}

Error: The local variable word may not have been initialized

Give the variable: word a value when you define it.

thanks 4 the reply...cn u gv an example of hw to define it???

Edited 4 Years Ago by Ezzaral: Do not write in all uppercase or use "leet", "txt" or "chatroom" speak.

Look at lines 18 & 19. They both define a variable and give it a value. Ignore the static final part.
<type> varName = <value>; // define a variable and give it a value

It seems that defining 'word' as an empty string is what you need:
word = "";
That is "s ; not 4 's
I.e. if you wanted to define it as 'apple' you would:
word = "apple";
By the way, if you can rename the subject of your post to include 'Javascript' it would help to attact the right experts.
Good luck
Steve

I'm going to disagree with the previous advice (sorry guys).
Initialising word to some arbitrary value will make the compile error go away, but it won't help your program to work properly.
Looking at the code, it looks like you want word to contain the text that the user typed into JTextField wordTF. So forget about initialising, and use the text from wordTF to set word before you try to parse it on 58.

And PCResolver: JavaScript???

ya...the compilation is complete bt the GUI didn't appear

Edited 4 Years Ago by kumaresen: hw can i use the text from wordTF to set word before i try to parse it on 58.wat should i do?can i have a guide or an example?

how can i use the text from wordTF to set word before i try to parse it on 58.can anione guide me?

okie...thank u...will try it out...can u tell me where should i put it in my coding?

Put it where you want to get the value from the textfield and when you expect there to be a value available because the user has clicked on a button telling you that the value is available.

This question has already been answered. Start a new discussion instead.