Hello guys. I wanted to know how I can go about writing what is selected in a Jcombobox to a datafile. This is an example of what I'm trying to do but when I compile i get an error. How can i correct the line "output.writeUTF(number.getSelectedIndex());"

String[] numbers = { "1","2", "3", "4", "5", "6" };
JComboBox number = new JComboBox(numbers);


if (check())
{
    try
    {
	output.writeUTF(number.getSelectedIndex());

        JOptionPane.showMessageDialog(null, "Your number has been saved.",    
        "Submission Successful", JOptionPane.INFORMATION_MESSAGE);
    }
    catch(IOException c)
    {
	System.exit(1);
    }
				
}

Well, what does writeUTF take as a parameter? (As a hint, here are the API docs for DataOutputStream http://java.sun.com/javase/6/docs/api/java/io/DataOutputStream.html).

And what does getSelectedIndex return? (As a hint, here are the API docs for JComboBox http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html.)

Now, what do you think is wrong?

Look closely at the docs for JComboBox. There is another getSelected.... method that will work (as long as the items in the JComboBox are Strings, and you cast the return Object properly) for you, find it.

I was unable to reach the first link. I think it's down. Also the only other one I can see apart from getSelectedIndex()is getSelectedItem(), and that doesn't work as well.

It might help to know what you mean by "it didn't work" and "i get an error".

Also, did you pay attention to the fact that I said you needed to do some casting?

Also, simply go here, http://java.sun.com/javase/6/docs/, and you can look up the classes yourself. You should already have this link bookmarked anyway.

Edit: And the first link didn't work, because it looks like the closing paren ")" is included in the link, and it shouldn't be.

Hello guys. I asked a week ago about how to write information selected from a jcombobox to a separate datafile. I've tried numerous things and have been unsuccessful. I've been at it for about a week now, trying to figure it out on my own but that also hasn't worked well. I wanted to know how I can go about doing this or if I'd need a separate class to change the combobox to something else. If so, may someone give me an example of doing it please. Thanks in advance.

Thanks alot. Sorry I angered you. I didn't know I had to add that (String) to it. That's where I was messing up. Now the proram works fine. Thanks again.

I didn't know I had to add that (String) to it.

Well, I did tell you, twice.

Look closely at the docs for JComboBox. There is another getSelected.... method that will work (as long as the items in the JComboBox are Strings, and you cast the return Object properly) for you, find it.

Also, did you pay attention to the fact that I said you needed to do some casting?

If you don't understand something you're told (i.e. didn't know what casting was), then ask about that specifically, otherwise we're all just wasting our time here.

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