---
title: "How can I convert a String to an int in Java?"  
description: "How can I convert a String to an int in Java?"  
author: "Utpal Vishwas"  
published: 2023-07-21  
updated: 2023-07-22  
canonical: https://www.mindstick.com/forum/159218/how-can-i-convert-a-string-to-an-int-in-java  
category: "java"  
tags: ["java", "string"]  
reading_time: 2 minutes  

---

# How can I convert a String to an int in Java?

How can I [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) a [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) to an [int](https://www.mindstick.com/forum/159137/how-to-convert-date-int-to-date-in-sql) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

There are a few ways to convert a string to an int in Java.

- **Use the** `Integer.parseInt()` **method.** The `Integer.parseInt()` method takes a string as a parameter and returns an int value. The string must be a valid representation of an integer.

Here is an example of how to use the `Integer.parseInt()` method:

```plaintext
String str = "123";
int value = Integer.parseInt(str);
```

- **Use the** `Integer.valueOf()` **method.** The `Integer.valueOf()` method takes a string as a parameter and returns an `Integer` object. The string must be a valid representation of an integer.

Here is an example of how to use the `Integer.valueOf()` method:

```plaintext
String str = "123";
Integer value = Integer.valueOf(str);
```

- **Use the** `Integer.parseInt()` **method with the** `try-catch` **block.** The `Integer.parseInt()` method can throw a `NumberFormatException` if the string is not a valid representation of an integer. You can use the `try-catch` block to handle the `NumberFormatException`.

Here is an example of how to use the `Integer.parseInt()` method with the `try-catch` block:

```plaintext
String str = "abc";

try {
  int value = Integer.parseInt(str);
} catch (NumberFormatException e) {
  System.out.println("The string is not a valid representation of an integer");
}
```

Which method you use to convert a string to an int depends on your specific needs. If you need to get an int value, then you can use the `Integer.parseInt()` method or the `Integer.valueOf()` method. If you need to be able to handle the `NumberFormatException`, then you can use the `Integer.parseInt()` method with the `try-catch` block.

Here are some important things to note about converting a string to an int:

- The string must be a valid representation of an integer.
- If the string is not a valid representation of an integer, the `Integer.parseInt()` method will throw a `NumberFormatException`.
- You can use the `Integer.parseInt()` method with the `try-catch` block to handle the `NumberFormatException`.


---

Original Source: https://www.mindstick.com/forum/159218/how-can-i-convert-a-string-to-an-int-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
