Programming
String to Int conversion
Coding Tutorial
Java
Data Types

How can I convert String to Int?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Java, converting a String to an int is usually done with Integer.parseInt. That sounds simple, but real code also has to deal with invalid input, whitespace, and numbers that may not fit inside the int range. A good solution therefore combines parsing with validation and clear error handling.

The Standard Java Conversion

The most direct conversion uses Integer.parseInt.

java
1public class Main {
2    public static void main(String[] args) {
3        String value = "123";
4        int number = Integer.parseInt(value);
5        System.out.println(number);
6    }
7}

This works when the string is a valid base-10 integer inside the int range.

If the string is not valid, Java throws NumberFormatException.

Handle Invalid Input Safely

In production code, you usually should not assume the text is always valid.

java
1public class Main {
2    public static void main(String[] args) {
3        String value = "12x";
4
5        try {
6            int number = Integer.parseInt(value);
7            System.out.println(number);
8        } catch (NumberFormatException ex) {
9            System.out.println("Invalid integer: " + value);
10        }
11    }
12}

This is the standard pattern when the input comes from a user, a file, or an API.

Trim Whitespace Before Parsing

Input often contains spaces around the number. parseInt does not automatically accept arbitrary surrounding whitespace in the way many developers expect, so trimming is a good habit.

java
1public class Main {
2    public static void main(String[] args) {
3        String value = " 42 ";
4        int number = Integer.parseInt(value.trim());
5        System.out.println(number);
6    }
7}

If you skip trim(), seemingly valid input may fail.

parseInt Versus valueOf

Java also provides Integer.valueOf.

java
String value = "55";
Integer boxed = Integer.valueOf(value);
int primitive = boxed;

The main difference is that parseInt returns a primitive int, while valueOf returns an Integer object. If you only need a primitive, parseInt is the clearer choice.

Other Bases And Numeric Formats

If the string is in another base, pass the radix explicitly.

java
1public class Main {
2    public static void main(String[] args) {
3        String binary = "1010";
4        int number = Integer.parseInt(binary, 2);
5        System.out.println(number);
6    }
7}

This prints 10 because the input is interpreted as binary.

That is useful when parsing hexadecimal, binary, or other fixed-base input formats.

Build A Helper When Parsing Happens Often

If your code parses user input frequently, a helper method can make the call sites cleaner.

java
1public class Main {
2    static Integer tryParseInt(String value) {
3        try {
4            return Integer.parseInt(value.trim());
5        } catch (NumberFormatException ex) {
6            return null;
7        }
8    }
9
10    public static void main(String[] args) {
11        System.out.println(tryParseInt("100"));
12        System.out.println(tryParseInt("oops"));
13    }
14}

Java does not have a built-in TryParse method like C#, so helpers like this are common when exceptions are not convenient at the call site.

Common Pitfalls

The most common mistake is assuming the string is always valid. Any non-numeric character, bad sign placement, or overflow will trigger NumberFormatException.

Another issue is forgetting to trim whitespace before parsing user input.

It is also easy to parse into the wrong numeric type. If the number may exceed int, use Long.parseLong or new BigInteger(...) instead.

Finally, do not confuse character codes with numeric parsing. Converting a string digit such as "7" to 7 is not the same as reading the Unicode value of the character.

Summary

  • Use Integer.parseInt for normal Java string-to-int conversion.
  • Wrap parsing in try/catch when input may be invalid.
  • Trim input first if it may contain surrounding whitespace.
  • Use valueOf only when you specifically want an Integer object.
  • Choose a larger numeric type if the input may exceed the int range.

Course illustration
Course illustration

All Rights Reserved.