Is it possible to change string to integer on Android?

Asked 1 years ago, Updated 1 years ago, 91 views

I'd like to change the string to an integer. There's edittext, and the user enters a number here. I received it as String hello, but I want to change it to Integer instead of String.

EditText et = (EditText) findViewById(R.id.entry1);
String hello = et.getText().toString();

By the way, how do I get the value of EditText as an Integer?

android-edittext string android java integer

2022-09-22 12:57

1 Answers

Integer.parseInt(et.getText().toString()); You can do it like this.

int myNum = 0;

try {
    myNum = Integer.parseInt(et.getText().toString());
} } catch(NumberFormatException nfe) {
   System.out.println("Could not parse " + nfe);
} 

I think it would be good to make an exception just in case.


2022-09-22 12:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.