Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
How do you convert a String to an Int in Ready To Program with Java?
I need to convert numbers in a string to numbers in an int.
For example:
String x;
x = "123";
needs to be changed to
x = 123;
NOTE: I have to convert a String array into an int.
And the string has commas in it (such as: 1,000,000)
3 Answers
- Anonymous7 years agoFavorite Answer
String str = "1234"
int num = Integer.parseInt(str);
- James BondLv 77 years ago
You need to process the string as you are specifying that the number may have commas.
However, do check NumberFormat class methods
- davidLv 77 years ago
"NOTE: I have to convert a String array into an int."
No problem. Use a loop.
int[] myInts = new int[myStrings.length];
for (int i = 0; i < myStrings.length; i++)
{
myInts[i] = Integer.parseInt(myStrings[i]);
}