0

I looked around for other questions but couldn't find one.

if I have an edit text and I enter a number how can I extract those numbers whether its a double/integer... (I have two edit texts called num1 and num2)

when I try and try to extract and check with java it isn't working.

I'm using toasts to check what happens. I will include some snapshots of what I have tried

Toast.makeText(this, "num1 is: "+parseDouble(String.valueOf(num1))  + "num2 is: "+parseDouble(String.valueOf(num1)) , Toast.LENGTH_SHORT).show();

here the program just crashes and doesn't work.

when I try:

Toast.makeText(this, "num1 is: "+(String.valueOf(num1))  + "num2 is: "+(String.valueOf(num1)) , Toast.LENGTH_SHORT).show();

this is my result on num1 and num2 from the edittext:

enter image description here

and when I try

Toast.makeText(this, "num1 is: "+num1  + "num2 is: "+num1 , Toast.LENGTH_SHORT).show();

I get the following:

enter image description here

to make it more clear here is how I input the numbers:

enter image description here

then enter the two numbers:

enter image description here

after I press the calculate sum button I get the toasts that I showed above, I did each one separately.

how can I extract num1 and num2 properly?(do i need to do a split on it with regex or is there a better way to do) let me know what options there are!

the problem isn't that it crashes, its why it doesn't extract the numbers properly like if I enter 2 into the edit text which the id is called num1 then num1 in the java( i have findviewid) should be 2...

limonik
  • 499
  • 1
  • 6
  • 28
Rocky cohn
  • 71
  • 1
  • 13
  • `here the program just crashes and doesn't work.` – a_local_nobody Jan 15 '21 at 08:10
  • Can you please add the stack trace when the app crashes and your Activity code to the question. – UrbanR Jan 15 '21 at 08:13
  • Looks like `num1` is an `EditText`. Why do you believe that `String.valueOf(num1)` would get the value entered by user? See duplicate link up top for how to get the value. – Andreas Jan 15 '21 at 08:23

2 Answers2

2
Double.parseDouble(String.valueOf(num1))

what does this do ?

you're trying to parse into a double, the string value of num1, but num1 is an edit text (a complete object), which is something you can't parse.

you have to use something like:

Double.parseDouble(foo.getText().toString())

what's the difference ? if you're calling String.valueOf(num1) you're getting the string value of the complete edit text object, however, if you're using .getText().toString() you're parsing the string value inside the edit text

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • 1
    no problem, i tried to explain it as best i could without just giving you the answer, if this has solved your problem you can mark it as an answer for others in future, happy coding :) – a_local_nobody Jan 15 '21 at 08:21
1

To extract the number from your editText , follow these steps :

  • Get Number from editText and convert it to int

// Make sure you typecast the values of your editText

int inputNumber = (int) editText.getText().toString()
  • Then simply show it in a your toast
Toast.makeText(this, "num1 is: + inputNumber" , Toast.LENGTH_SHORT).show();
  • Calculating Two Numbers
int inputNumber1 = (int) editText.getText().toString();
int inputNumber2 = (int) editText.getText().toString();
int sum = inputNumber1 + inputNumber2;

PS : For your app to not crash , make sure you concat a string with your int
value , for example ( "" + sum )

Taki
  • 3,290
  • 1
  • 16
  • 41