Android how to convert int to String? [duplicate] - android

This question already has answers here:
How do I convert from int to String?
(20 answers)
Closed 9 years ago.
I have an int and I want to convert it to a string. Should be simple, right? But the compiler complains it can't find the symbol when I do:
int tmpInt = 10;
String tmpStr10 = String.valueOf(tmpInt);
What is wrong with the above? And, how do I convert an int (or long) to a String?
Edit: valueOf not valueof ;)

Use this String.valueOf(value);

Normal ways would be Integer.toString(i) or String.valueOf(i).
int i = 5;
String strI = String.valueOf(i);
Or
int aInt = 1;
String aString = Integer.toString(aInt);

You called an incorrect method of String class, try:
int tmpInt = 10;
String tmpStr10 = String.valueOf(tmpInt);
You can also do:
int tmpInt = 10;
String tmpStr10 = Integer.toString(tmpInt);

Use Integer.toString(tmpInt) instead.

Related

Convert String same as into Int in Android

I want to convert String into int.
For example:
if String is "0x1F60A" then it should be convert into int same as 0x1F60A
String str = "0x1F60A";
int i = 0x1F60A; // it must be something like this.
Why I need this. Because I'm going to convert unicode int into Emoji
I'm using this code.
final String emojiString = str.substring(start,end);
// value in emojiString is 0x1F60A
int unicode = // need to convert this string into int
ss.replace(start, end, new String(Character.toChars(unicode)));
Can you please let me know how can I do that.
For conversion of Unicode to Int:
You have to omit the 2 first characters with .substring(2)
int code = Integer.parseInt(unicodeStr.substring(2), 16);
For conversion of String to int:
try {
myNum = Integer.parseInt(myString);
} catch(NumberFormatException nfe) {
}
int unicode = Integer.parseInt(emojiString.substring(2), 16);

How to get split string with special characters in android?

I have a string named namely "-10.00","-100.00","-1000.00". I want to get value like "10","100","1000" from that string. I have tried to get substring but did not able to get.
code i have tried
String amount = "-10.00";
String trimwalletBalance = amount.substring(0, amount.indexOf('.'));
From above i only get "-10".
String trimwalletBalance = amount.substring(1, amoun.indexOf("."));
Its very simple.
Do it like String trimwalletBalance = amount.substring(1, amount.indexOf('.'));
Instead of position 0, You should get substring from position 1
Convert it into integer and then name it positive:
String amount = "-10.00";
int amountInt = (int) Double.parseDouble(amount);
if(amountInt<0)amountInt*=-1;
Try
String amount = "-10.00";
int value = (int) Double.parseDouble(amount);
if(value < 0) value *= -1;
//value will be 10
OR
String text = amount.substring(1, amount.indexOf('.'));

string to int,causing app to stop

final EditText ed7 = (EditText)findViewById(R.id.Recycleweight);
final EditText ed8 = (EditText)findViewById(R.id.Nonweight);
final EditText ed11 = (EditText)findViewById(R.id.totalweight);
recy_wt=ed7.getText().toString();
nonrec_wt=ed8.getText().toString();
total_wt=ed11.getText().toString();
int rec = Integer.parseInt(recy_wt);
int nrec = Integer.parseInt(nonrec_wt);
ed11.setText(String.valueOf(rec + nrec));
The input I am giving is,ed7=10kg and ed8=20kg,and it will be displayed in ed11
as 30kg,but the app is unfortunately stopping.
If I am not writing 'kg',it is correctly displaying the totalwt.
But I want to display 'kg' in the answer.
There is already a solution here (can't flag as duplicate)
Find and extract a number from a string
Extract the digit part from the string and then parse it to an integer
Try this out
String regexp = "/^[0-9]+kg$/g"; //It accepts only '<number><kg>' format
int weight = 0;
if (recy_wt.matches(regexp) && nonrec_wt.matches(regex)) {
//It's valid input
Scanner scan = new Scanner(recy_wt);
weight = Integer.parseInt(scan.findInLine("\\d+(\\.\\d+)?"));
Scanner scan = new Scanner(nonrec_wt);
weight += Integer.parseInt(scan.findInLine("\\d+(\\.\\d+)?"));
}
ed11.setText(String.valueOf(weight + "Kg"));
If the only possible unit is kg, then try like this
final EditText ed7 = (EditText)findViewById(R.id.Recycleweight);
final EditText ed8 = (EditText)findViewById(R.id.Nonweight);
final EditText ed11 = (EditText)findViewById(R.id.totalweight);
int weight = 0;
recy_wt=ed7.getText().toString();
nonrec_wt=ed8.getText().toString();
recy_wt = recy_wt.replace("kg","");
nonrec_wt = nonrec_wt.replace("kg","");
weight += Integer.parseInt(recy_wt) + Integer.parseInt(nonrec_wt);
ed11.setText(weight + "kg");
This will work:-
recy_wt=ed7.getText().toString();
nonrec_wt=ed8.getText().toString();
String n=recy_wt.replace("kg","");
String n1=nonrec_wt.replace("kg","");
weight += Integer.parseInt(n) + Integer.parseInt(n1);
ed11.setText(weight + "kg");
storing the replaced one in a string and then converting to int will help.

How to add one to value of EditText in Android

I am trying to get the string from an EditText and add 1 to the value on a button click but instead of doing 1+1 and setting it to 2, it outputs to the EditText 1111 and so on. I need some help getting the math working. Thanks in advance!
int rentPrice = 0;
int peopleCount = 0;
rentPrice = Integer.parseInt(mRentPriceText.getText().toString());
mybutton.setOnClickListent(new OnClickListener(){
mRentPriceText.setText(rentPrice+1);
}
String actually concats 1. so "1"+"1"="11".
So you have to first convert it to int and add 1 and then reconvert it to String and set it to EditText.
So change this line
mRentPriceText.setText(rentPrice+1);
to
mRentPriceText.setText(Integer.toString(rentPrice+1));
Try this.
int temp = 0;
int rentPrice = 0;
int peopleCount = 0;
rentPrice = Integer.parseInt(mRentPriceText.getText().toString());
temp =rentPrice+1
mybutton.setOnClickListent(new OnClickListener(){
mRentPriceText.setText(String.valueOf(temp));
}
Try like this:
int rentPrice = 0;
int peopleCount = 0;
mybutton.setOnClickListent(new OnClickListener(){
rentPrice = Integer.parseInt(mRentPriceText.getText().toString());
int out = rentPrice+1;
mRentPriceText.setText(String.valueOf(out));//Convert out to string
}
If you gave int value as set text then it will find some string resource so cast int to string and use inner () add +1 to rentPrice :
mRentPriceText.setText(String.valueOf((rentPrice+1)));
If you are doing mTextview.setText(someInteger) you are more likely to get the below exception
android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:1134)
at android.widget.TextView.setText(TextView.java:4940)
2
because the TextView.setText(int) makes android think that you might be referring to some R.string.some_string . So wrap up your calculation work in the String as String.valueOf("some calculation")
Also I have some Tip for you
don't use toString() in line rentPrice = Integer.parseInt(mRentPriceText.getText().toString()); otherwise you would more likely get some time NullPointerException
Instead always go with
rentPrice = Integer.parseInt(String.valueOf(mRentPriceText.getText()));

int to String in Android

I get the id of resource like so:
int test = (context.getResourceId("raw.testc3"));
I want to get it's id and put it into a string. How can I do this? .toString does not work.
String testString = Integer.toString(test);
Or you can use Use
String.valueOf()
eg.
int test=5;
String testString = String.valueOf(test);
Very fast to do it if you dnt remember or dnt have time to type long text :
int a= 100;
String s = ""+a;
What about:
int a = 100;
String s = String.format("%d", a);

Categories

Resources