0

I am learning java , I want to make one simple calculator program using GUI in Java. Idea is simple.

Calculator has 2 JTextField for 1st number and for 2nd number.

When I run that program will show -- "Enter the First Number" , below that JTextField to enter 1st number , below that "Enter the Second Number" , below that JTextField to enter 2nd number.

My doubt/question is How to get those input from JTextField and assign it to Scanner input1 and Scanner input2 ? Is it possible to do that ? If not what are the alternative ways ?

Hope you will understand my question , if you are in-front of me I hope I will explain it more properly .

Praneeth Peiris
  • 2,008
  • 20
  • 40

2 Answers2

2

You don't need a Scanner to read data from a JTextField.

If the identifier of the JTextField is jt, you simply can,

String text = jt.getText();
Praneeth Peiris
  • 2,008
  • 20
  • 40
0
String text = jtext.getText().toString();

or if you want a number

int num = Integer.parseInt(jtext.getText().toString());

The .toString() is redundant but it makes the code easier to read and you will thank yourself later too!

rush2sk8
  • 362
  • 6
  • 16