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)
    {

    }
}

Recommended Answers

All 14 Replies

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???

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

thanks everyone

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

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

text fields have a getText() method that you can use to obtain the value you want in word.

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.

okie...thank u...will try it out...hope it works...

didnt work...=(

Can you explain what happened and post the code to show the problem?

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.