How to convert a string to an integer in Java?

Report
Question

Please briefly explain why you feel this question should be reported .

Report
Cancel

Hello friends,

I have a  string that contains only numbers and I want to get its number representation.

For example,

str1 = "8900", the result will be the number 8900.

Thanks.

solved 0
Java 4 weeks 2 Answer 1401 views 0

About john

Answers ( 2 )

  1. Please briefly explain why you feel this answer should be reported .

    Report
    Cancel

    use Integer.parseInt(str1)

  2. rg123

    Please briefly explain why you feel this answer should be reported .

    Report
    Cancel

    There are two ways to convert a string to an integer in Java?

    1) Integer a = Integer.valueOf(str);

    2) int a = Integer.parseInt(str);

    ————————————————————————
    valueOf(int i)
    The method returns an Integer instance representing the specified int value.
    Parameters:
    i – an int value.
    ————————————————————————
    parseInt(String s)
    The method parses the string argument as a signed decimal integer.
    Parameters:
    s – a String containing the int representation to be parsed

    Best answer

Leave an answer

Browse

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>