# How to convert a string to int in Java

If you want to convert a string to int in Java, you have two simple options – `parseInt()` und `valueOf()`. However, both of these options only work if the string exclusively contains whole numbers and doesn’t exceed the value range of the int data type.

## What is a string to int conversion in Java?

[Java strings](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-strings/) are concatenations that can contain characters, digits and special characters. In the Java programming language, digits in the form of integer values can be saved as an int. If you have a string that only contains whole numbers, you can convert it into the [Java primitive](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-primitives/) int, which stands for integer. While there are 5 different ways to convert a [Java int to a string](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-int-to-string/), there are only **2 main ways to convert a Java string to an int**. They are `Integer parseInt()` and `Integer.valueOf()`. Below we’ll explain how these methods work using practical examples.

Note Note that this conversion will only work if the number in question is between **-2147483648 and 2147483647**. Numbers that are larger or smaller can’t be represented using the int data type.

## How to use `Integer.parseInt()`

`parseInt()` is a **static method** that belongs to the Integer class and returns the numerical value of the complex data type string in the form of the primitive data type int. Its syntax looks as follows:

```java
static int parseInt(String s)
```

Let’s look at an example. First we’ll initialise a string that contains the value ‘7312’ and nothing else. Then we’ll convert that Java string to an int using `parseInt()`, and finally we’ll output the result using the Java command `System.out.println`. The code will look as follows:

```java
public class Main {
	public static void main(String args[]) {
		String str = "7312";
		int number = Integer.parseInt(str);
		System.out.println("The numerical value of the string is: " + number);
	}
}
```

Here’s the output:

```java
The numerical value of the string is: 7312
``
However, this only works if the string exclusively **contains whole numbers**. If it contains anything else, you’ll get an error message:
```java
public class Main {
public static void main(String args[]) {
String str = "The number is 7312";
int number = Integer.parseInt(str);
System.out.println("The number from the string is: " + number);
}
}
```

</div>Here’s the error message:

<div class="prism-container position-relative my-4" data-ctype="commonmark_content">```java
Exception in thread "main" java.lang.NumberFormatException: For input string: " The number is 7312"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:661)
at java.base/java.lang.Integer.parseInt(Integer.java:777)
at Main.main(Main.java:4)
```

</div>## How to use `Integer.valueOf()`

`Integer.valueOf()`is another method from the Integer class. It outputs the **decimal interpretation of a string object** and is thus also useful for converting a Java string to an int. Its syntax looks as follows:

<div class="prism-container position-relative my-4" data-ctype="commonmark_content">```java
static int valueOf(int a)
```

</div>Let’s turn to a simple example:

<div class="prism-container position-relative my-4" data-ctype="commonmark_content">```java
public class Main {
public static void main(String args[]) {
String newString = "7312";
int parNumber = Integer.valueOf(newString);
System.out.println("The numerical value of the string is: " + parNumber);
}
}
```

</div>You’ll again get the following result:

<div class="prism-container position-relative my-4" data-ctype="commonmark_content">```java
The numerical value of the string is: 7312
```

If the string contains anything except for a whole number, we’ll get the same error message as we got above.


This is a markdown version of: [https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-string-to-int/](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-string-to-int/) for AI/LLM consumption.