JohnSnow
JohnSnow

Reputation: 173

how to assign String type data from JTextField to a variable

I can successfully do this for int but when I try to do it for text2var and text3var (which are String) it does not work. Error below, same thing occur for text3var.

try
{

    text1var = Integer.parseInt(text1.getText());
    text2var = text2var.getText();
    text3var = text3var.getText();
    text4var = Integer.parseInt(text4.getText());

    output = new DataOutputStream(new FileOutputStream("datafile.dat") );
    output.writeInt(text1var);
    output.writeUTF(text2var);
    output.writeUTF(text3var);
    output.writeInt(text4var);
    output.close();
}

enter image description here

Upvotes: 0

Views: 50

Answers (1)

nhouser9
nhouser9

Reputation: 6780

The error is pretty much exactly what the error message says it is.

text2var is not a JTextField, it is a string. Try calling getText() on a JTextField, maybe text2?

Upvotes: 3

Related Questions