Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting integer values from textfield

I am trying to get Integer values from my jtextfield but not able to it is showing incompatible datatypes required int found string. Is there some other way of writing my code is as follows and i want to get only integer values

private void jTextField2MouseClicked(java.awt.event.MouseEvent evt) {
        if(evt.getSource()==jTextField2){
            int jml = jTextField3.getText();
            jTextField1.setText(numberToWord(jml));

        }
    }
like image 474
Sumeet Gavhale Avatar asked Jan 05 '13 06:01

Sumeet Gavhale


2 Answers

You need to use Integer.parseInt(String)

private void jTextField2MouseClicked(java.awt.event.MouseEvent evt) {
        if(evt.getSource()==jTextField2){
            int jml = Integer.parseInt(jTextField3.getText());
            jTextField1.setText(numberToWord(jml));

        }
    }
like image 141
Rahul Avatar answered Sep 21 '22 07:09

Rahul


As You're getting values from textfield as jTextField3.getText();.

As it is a textField it will return you string format as its format says:

String getText()

      Returns the text contained in this TextComponent.

So, convert your String to Integer as:

int jml = Integer.parseInt(jTextField3.getText());

instead of directly setting

   int jml = jTextField3.getText();
like image 32
Parth Soni Avatar answered Sep 22 '22 07:09

Parth Soni