1
votes

I need to get an integer through a textfield.

textField_1 = new JTextField();
textField_1.addInputMethodListener(new InputMethodListener() {
    public void caretPositionChanged(InputMethodEvent e) {
        //protected void processInputMethodEvent(InputMethodEvent e){
            //InputMethodListener listener=inputMethodListener;
            //op = textField_1.getText();
            //inputnum = Integer.parseInt ( op );
            inputnum = Integer.parseInt(textField_1.getText());
            textField.setText ( String.format ( "%d + %d = ", inputnum ) );
            if(inputnum == numA ){
                score += 10;
                Correctness.setText("That's Correct!");
                correct = 1;
            }
            else{Correctness.setText("That's Incorrect!");
                } 
        }

        public void inputMethodTextChanged(InputMethodEvent arg0) {
            //inputnum = console.nextInt();     
        }
    }
}

this is the code here. so i want to be able to input an integer and then have it set to inputnum. i hope that someone here can help me because this and anot bit is due in about 4 hours

1
use scanner to get input from user. - Sajan Chandran
i don't get any error i just edited out a part that i tried. it just doesn't get any input - esmaj

1 Answers

0
votes

Retrieve input as string and then convert it to integer
Integer.ParseInt(String); //convert string to integer