# How to convert a string to an integer in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Aug 24, 2019, updated Oct 29, 2022. Topics: Java.
Canonical URL: https://attacomsian.com/blog/convert-string-to-integer-in-java

> A quick article to learn how to convert a string to an integer in Java.

A string to an integer conversion is one of the most used operations while writing code. There are multiple ways available to do this simple conversion in Java. 

Let us go through these methods one by one. If you want to convert an integer to a string, read [this article](https://attacomsian.com/blog/java-convert-integer-to-string).

## Convert a string to an integer using `Integer.parseInt()`

The `parseInt()` static method from [Integer](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html) class converts a string into a primitive integer (`int`). It takes two arguments. The first argument is the string to convert. The second optional argument is the base number called radix:

```java
String str = "85";
int num = Integer.parseInt(str);
System.out.println(num); // 85

// with radix 16
String strBase16 = "100";
int num2 = Integer.parseInt(strBase16, 16);
System.out.println(num2); // 256
```

If the string contains non-digit characters, even a dot (`.`), the `parseInt()` method throws a `NumberFormatException`:

```java
String str = "19.49";
int price = Integer.parseInt(str);
System.out.println(price);
```

Here is the output for the above conversion:

```bash
Exception in thread "main" java.lang.NumberFormatException: For input string: "19.49"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
```

## Convert a string to an integer using `Integer.valueOf()`

The `valueOf()` static method converts a string into an integer. But it, unlike the `parseInt()` method, returns an `Integer` object instead of a primitive `int` value. The `NumberFormatException` is thrown if the string is not a parsable integer:

```java
Integer num1 = Integer.valueOf("19");
System.out.println(num1); // 19

Integer num2 = Integer.valueOf("20", 16);
System.out.println(num2); // 32

Integer num3 = Integer.valueOf("49%"); // NumberFormatException
System.out.println(num3);
```

## Convert a string to an integer using `Integer.decode()`

The `decode()` static method accepts one parameter, a string, and decodes it into an `Integer` object. If the string is not parsable, a `NumberFormatException` is thrown:

```java
Integer num1 = Integer.decode("489");
System.out.println(num1); // 489

Integer num2 = Integer.decode("41");
System.out.println(num2); // 41

Integer num3 = Integer.decode("4MB"); // NumberFormatException
System.out.println(num3);
```

## Convert a string to an integer using `Scanner`

Although not very popular, the [Scanner](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html) class can also be used to convert a string into an integer. The `nextInt() ` method scans the next token of the input as a primitive `int`:

```java
System.out.println(new Scanner("124").nextInt()); // 124
System.out.println(new Scanner("189").nextInt()); // 189
```

## `NumberFormatException` Exception

The `Integer` class methods (`parseInt()`, `valueOf()`, `decode()`) throw a `NumberFormatException` if the string is not a parsable integer. You use the `try-catch` block to handle this exception:

```java
try {
    String str = "9.49";
    int price = Integer.parseInt(str);
    System.out.println(price);
} catch (NumberFormatException ex) {
    ex.printStackTrace();
}
```

Read [this guide](https://attacomsian.com/blog/data-type-conversions-java) to learn about other data type conversions like string to date, a string to float, a string to double, and more in Java.
