coding
int
string
java

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

Master System Design with Codemia

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

In Java, you can convert a String to an int using several methods. Below are the most common approaches:


1. Using Integer.parseInt()

The Integer.parseInt() method is the most straightforward way to convert a String to an int.

Example:

java
1String numberStr = "123";
2int number = Integer.parseInt(numberStr);
3
4System.out.println(number); // Output: 123

Handling Exceptions:

If the input string is not a valid integer, it throws a NumberFormatException.

java
1try {
2    String invalidStr = "abc";
3    int number = Integer.parseInt(invalidStr);
4} catch (NumberFormatException e) {
5    System.out.println("Invalid integer format");
6}

2. Using Integer.valueOf()

The Integer.valueOf() method also converts a String to an int, but it returns an Integer object (not a primitive int).

Example:

java
1String numberStr = "456";
2int number = Integer.valueOf(numberStr);
3
4System.out.println(number); // Output: 456
  • Auto-unboxing happens when assigning an Integer object to a primitive int.

3. Using Integer.decode()

Integer.decode() can parse numbers from a String, including those with prefixes like "0x" (hexadecimal), "0" (octal), or plain decimal numbers.

Example:

java
1String numberStr1 = "123";
2String numberStr2 = "0x7B"; // Hexadecimal
3String numberStr3 = "0173"; // Octal
4
5int decimal = Integer.decode(numberStr1);
6int hex = Integer.decode(numberStr2);
7int octal = Integer.decode(numberStr3);
8
9System.out.println(decimal); // Output: 123
10System.out.println(hex);     // Output: 123
11System.out.println(octal);   // Output: 123

4. Using parseInt() with a Radix

If the string represents a number in a specific base (radix), you can use Integer.parseInt() with a second argument for the radix.

Example:

java
1String binaryStr = "1010"; // Binary
2String hexStr = "7B";      // Hexadecimal
3
4int binary = Integer.parseInt(binaryStr, 2); // Base 2
5int hex = Integer.parseInt(hexStr, 16);      // Base 16
6
7System.out.println(binary); // Output: 10
8System.out.println(hex);    // Output: 123

5. Using Scanner Class

You can use the Scanner class to read and parse integers from strings.

Example:

java
1import java.util.Scanner;
2
3String numberStr = "789";
4Scanner scanner = new Scanner(numberStr);
5int number = scanner.nextInt();
6
7System.out.println(number); // Output: 789

Summary Table

MethodDescriptionOutput
Integer.parseInt()Converts String to primitive int.Primitive int
Integer.valueOf()Converts String to Integer object.Integer object
Integer.decode()Parses numbers in decimal, octal, or hexadecimal formats.Primitive int
parseInt(String, radix)Converts String to int with a specified base (radix).Primitive int
Scanner.nextInt()Parses integers using the Scanner class.Primitive int

Notes:

  • Always handle NumberFormatException when converting strings to integers to ensure the input is valid.
  • Use Integer.parseInt() for most cases where you need a primitive int.

By choosing the appropriate method, you can safely and effectively convert a String to an integer in Java. 🚀


Course illustration
Course illustration

All Rights Reserved.