How to convert a String to an int in Java?

Spread the love

Question Description

How can I convert a String to an int in Java?

My String contains only numbers and I want to return the number it represents.

For example, given the string "1234" the result should be the number 1234.

Practice As Follows

int foo = Integer.parseInt("1234");

See the Java Documentation for more information.

(If you have it in a StringBuilder (or the ancient StringBuffer), you’ll need to do Integer.parseInt(myBuilderOrBuffer.toString()); instead).

Leave a Comment