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.

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;

Update:

NOTE: I have to convert a String array into an int.

Update 2:

And the string has commas in it (such as: 1,000,000)

3 Answers

Relevance
  • Anonymous
    7 years ago
    Favorite Answer

    String str = "1234"

    int num = Integer.parseInt(str);

  • 7 years ago

    You need to process the string as you are specifying that the number may have commas.

    However, do check NumberFormat class methods

  • david
    Lv 7
    7 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]);

    }

Still have questions? Get your answers by asking now.