Reputation: 7243
How do I convert a string into an integer?
I have a textbox I have the user enter a number into:
EditText et = (EditText) findViewById(R.id.entry1);
String hello = et.getText().toString();
And the value is assigned to the string hello
.
I want to convert it to a integer so I can get the number they typed; it will be used later on in code.
Is there a way to get the EditText
to a integer? That would skip the middle man. If not, string to integer will be just fine.
Upvotes: 206
Views: 678435
Reputation: 67
I have simply change this
int j = Integer.parseInt(user.replaceAll("[\\D]", ""));
into
int j = 1;
AND IT SOLVE MY PROBLEM
Upvotes: 2
Reputation: 51
There are five ways to convert The First Way :
String str = " 123" ;
int i = Integer.parse(str);
output : 123
The second way :
String str = "hello123world";
int i = Integer.parse(str.replaceAll("[\\D]" , "" ) );
output : 123
The Third Way :
String str"123";
int i = new Integer(str);
output "123
The Fourth Way :
String str"123";
int i = Integer.valueOf(Str);
output "123
The Fifth Way :
String str"123";
int i = Integer.decode(str);
output "123
There could be other ways But that's what I remember now
Upvotes: 5
Reputation: 58934
There are available Extension methods to parse them into other primitive types.
"10".toInt()
"10".toLong()
"true".toBoolean()
"10.0".toFloat()
"10.0".toDouble()
"10".toByte()
"10".toShort()
String num = "10";
Integer.parseInt(num );
Upvotes: 3
Reputation: 695
The much simpler method is to use the decode
method of Integer
so for example:
int helloInt = Integer.decode(hello);
Upvotes: 3
Reputation: 63662
See the Integer class and the static parseInt()
method:
http://developer.android.com/reference/java/lang/Integer.html
Integer.parseInt(et.getText().toString());
You will need to catch NumberFormatException
though in case of problems whilst parsing, so:
int myNum = 0;
try {
myNum = Integer.parseInt(et.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
Upvotes: 468
Reputation: 2902
Try this code it's really working.
int number = 0;
try {
number = Integer.parseInt(YourEditTextName.getText().toString());
} catch(NumberFormatException e) {
System.out.println("parse value is not valid : " + e);
}
Upvotes: 8
Reputation: 4108
You can use the following to parse a string to an integer:
int value=Integer.parseInt(textView.getText().toString());
(1) input: 12 then it will work.. because textview has taken this 12 number as "12" string.
(2) input: "abdul" then it will throw an exception that is NumberFormatException. So to solve this we need to use try catch as I have mention below:
int tax_amount=20;
EditText edit=(EditText)findViewById(R.id.editText1);
try
{
int value=Integer.parseInt(edit.getText().toString());
value=value+tax_amount;
edit.setText(String.valueOf(value));// to convert integer to string
}catch(NumberFormatException ee){
Log.e(ee.toString());
}
You may also want to refer to the following link for more information: http://developer.android.com/reference/java/lang/Integer.html
Upvotes: 5
Reputation:
Use regular expression is best way to doing this as already mentioned by ashish sahu
public int getInt(String s){
return Integer.parseInt(s.replaceAll("[\\D]", ""));
}
Upvotes: 9
Reputation: 151
You can also do it one line:
int hello = Integer.parseInt(((Button)findViewById(R.id.button1)).getText().toString().replaceAll("[\\D]", ""));
Reading from order of execution
findViewById(R.id.button1)
((Button)______)
to cast the View
as a Button
.GetText()
to get the text entry from Button.toString()
to convert the Character Varying to a String.ReplaceAll()
with "[\\D]"
to replace all Non Digit Characters with "" (nothing)Integer.parseInt()
grab and return an integer out of the Digit-only string.Upvotes: 4
Reputation: 1558
Use regular expression:
String s="your1string2contain3with4number";
int i=Integer.parseInt(s.replaceAll("[\\D]", ""));
output: i=1234;
If you need first number combination then you should try below code:
String s="abc123xyz456";
int i=NumberFormat.getInstance().parse(s).intValue();
output: i=123;
Upvotes: 28
Reputation: 1558
Use regular expression:
int i=Integer.parseInt("hello123".replaceAll("[\\D]",""));
int j=Integer.parseInt("123hello".replaceAll("[\\D]",""));
int k=Integer.parseInt("1h2el3lo".replaceAll("[\\D]",""));
output:
i=123;
j=123;
k=123;
Upvotes: 13
Reputation: 35549
Best way to convert your string into int is :
EditText et = (EditText) findViewById(R.id.entry1);
String hello = et.getText().toString();
int converted=Integer.parseInt(hello);
Upvotes: 7
Reputation: 61
You should covert String to float. It is working.
float result = 0;
if (TextUtils.isEmpty(et.getText().toString()) {
return;
}
result = Float.parseFloat(et.getText().toString());
tv.setText(result);
Upvotes: 5
Reputation: 1840
int in = Integer.valueOf(et.getText().toString());
//or
int in2 = new Integer(et.getText().toString());
Upvotes: 51