|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "How to covert String to Integer in Java" |
| 4 | +author: gaurav |
| 5 | +categories: [ Java, Core Java, String] |
| 6 | +description: "In this article you are going to learn how can one convert the string into an integer using the inbuilt methods of the Integer class." |
| 7 | +--- |
| 8 | + |
| 9 | +In this article you are going to learn how can one convert the string into an integer using the inbuilt methods of the Integer class. |
| 10 | + |
| 11 | +## Introduction |
| 12 | +We have learned many tutorials on the String class and seen the String operations are an important part of java programming. |
| 13 | + |
| 14 | +Sometimes we took some values as a String but we want to operate them as an Integer, then questions arise how can one convert string to the integer? The Java team provided the solution for it. |
| 15 | + |
| 16 | +Converting a string to integer is one of the most important things to know by a Java Developer. |
| 17 | + |
| 18 | +Now we will see what are the available options to convert a Siring into Integer. |
| 19 | + |
| 20 | +I have listed down two ways to convert a String into an Integer. |
| 21 | + |
| 22 | +1. Using the `Integre.parseInt()` method |
| 23 | +2. Using the `Integer.valueOf()` method |
| 24 | + |
| 25 | +Now we will see the above ways to convert string to integer one by one. |
| 26 | + |
| 27 | +## Using the `Integre.parseInt()` method |
| 28 | + |
| 29 | +We can convert the String to an integer using the `Integre.parseInt()` method. |
| 30 | + |
| 31 | +The `Integer.parseInt()` method can convert an entire string to integer provided the String must contain the digit values only. For example `String str= "123";` |
| 32 | + |
| 33 | +Now we will see one example which converts the string to an integer using the `Integre.parseInt()` method. |
| 34 | + |
| 35 | +```java |
| 36 | +/* |
| 37 | + * A Java program to convert String to Integer. |
| 38 | + * |
| 39 | + * @author Gaurav Kukade at coderolls.com |
| 40 | + */ |
| 41 | +public class StringToInt{ |
| 42 | + public static void main(String [] args){ |
| 43 | + |
| 44 | + String str = "1254"; |
| 45 | + |
| 46 | + // convert string to int |
| 47 | + int result = Integer.parseInt(str); |
| 48 | + |
| 49 | + System.out.println(result); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | +Output |
| 54 | +```java |
| 55 | +1254 |
| 56 | +``` |
| 57 | +## Using the `Integer.valueOf()` method |
| 58 | + |
| 59 | +We can also convert String to Integer using the `Integer.valueOf()` method. |
| 60 | + |
| 61 | +The `Integer.valueOf()` method returns the integer instance of the value represented by the string variable. |
| 62 | + |
| 63 | +Below we will see one program to convert string to int using the `Integer.valueOf()` method. |
| 64 | + |
| 65 | +```java |
| 66 | +/* |
| 67 | + * A Java program to convert String to Integer. |
| 68 | + * |
| 69 | + * @author Gaurav Kukade at coderolls.com |
| 70 | + */ |
| 71 | +public class StringToInt{ |
| 72 | + public static void main(String [] args){ |
| 73 | + |
| 74 | + String str = "134"; |
| 75 | + |
| 76 | + // convert string to int |
| 77 | + int result = Integer.valueOf(str); |
| 78 | + |
| 79 | + System.out.println(result); |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | +Output |
| 84 | +```java |
| 85 | +134 |
| 86 | +``` |
| 87 | + |
| 88 | +## Let us know about `NumberFormatException` |
| 89 | + |
| 90 | +When you are trying to convert a string to integer but your string contains a value other than the decimal digits, then bothe the methods (`Integre.parseInt()` method and `Integer.valueOf()` method) will throw the `NumberFormatException`. |
| 91 | + |
| 92 | +Below we will one program to see how `Integre.parseInt()` method throw the `NumberFormatException`. |
| 93 | + |
| 94 | +```java |
| 95 | +/* |
| 96 | + * A Java program showing the Integer.parseInt() method throwing |
| 97 | + * NumberFormatException |
| 98 | + * |
| 99 | + * @author Gaurav Kukade at coderolls.com |
| 100 | + */ |
| 101 | +public class StringToInt{ |
| 102 | + public static void main(String [] args){ |
| 103 | + |
| 104 | + String str = "134xyz"; |
| 105 | + |
| 106 | + // convert string to int |
| 107 | + int result = Integer.valueOf(str); |
| 108 | + |
| 109 | + System.out.println(result); |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | +Output |
| 114 | +```java |
| 115 | +Exception in thread "main" java.lang.NumberFormatException: For input string: "134xyz" |
| 116 | + at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) |
| 117 | + at java.lang.Integer.parseInt(Integer.java:580) |
| 118 | + at java.lang.Integer.valueOf(Integer.java:766) |
| 119 | + at StringToInt.main(StringToInt.java:14) |
| 120 | +``` |
| 121 | + |
| 122 | +## Conclusion |
| 123 | + |
| 124 | +We can convert String to integer using two methods, `Integre.parseInt()` method and `Integer.valueOf()` method. |
| 125 | + |
| 126 | +If the string contains some other value than the decimal numbers bothe the methods will throw the `NumberFormatException`. |
| 127 | + |
| 128 | +If you found this article worth, support me by [giving a cup of Coffee ☕](https://www.paypal.me/GauravKukade) |
| 129 | + |
| 130 | +If you know any other way to convert the string to integer or if you have any query about the string to integer conversion please write it down in the comment section below. |
0 commit comments