String to int in Java Last Updated : 22 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Converting a String to an int in Java can be done using methods provided in the Integer class, such as Integer.parseInt() or Integer.valueOf() methods. Example:The most common method to convert a string to a primitive int is Integer.parseInt(). It throws a NumberFormatException if the string contains non-numeric characters. Java // Java Program to demonstrate // String to int conversion using parseInt() public class StringToInt { public static void main(String[] args) { String s = "12345"; // Convert the string to an integer // using Integer.parseInt() int n = Integer.parseInt(s); System.out.println("" + n); } } Output12345 Other Ways to Convert String to int Using Integer.valueOf() MethodThe Integer.valueOf() method converts a String to an Integer object instead of a primitive int. We can unbox it to an int.Note: valueOf() method uses parseInt() internally to convert to integer. Example: Java // Java ProgramConvert String to int // using Integer.valueOf() Method public class StringToInt { public static void main(String[] args) { // Convert String to Integer using valueOf() String s = "217"; // Convert the string to an Integer object // using Integer.valueOf() int n = Integer.valueOf(s); System.out.println("" + n); } } Output217 Handling Errors for Non-Numeric StringsIf a string literal does not contain numeric values, calling the Integer.parseInt() or Integer.valueOf() methods will result in a NumberFormatException.Example: Java // Java Program to Demonstrate Working of parseInt() Method // where NumberFormatException is Thrown public class StringToIntExample { public static void main(String[] args) { // NumberFormatException due to empty string String s1 = ""; int n1 = Integer.parseInt(s1); // NumberFormatException due to non-numeric string String s2 = "geeksforgeeks"; int n2 = Integer.parseInt(s2); System.out.println(n1); System.out.println(n2); } } Output:Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:662) at java.base/java.lang.Integer.parseInt(Integer.java:770) at StringToIntExample.main(StringToIntExample.java:10) Comment More infoAdvertise with us Next Article String to int in Java kartik Follow Improve Article Tags : Java Computer Science Fundamentals Java-Strings Java-lang package Java-Functions Java-Integer +2 More Practice Tags : JavaJava-Strings Similar Reads Integer toString() in Java The java.lang.Integer.toString() is an inbuilt method in Java which is used to return the String object representing this Integer's value. Syntax : public static String toString() Parameters: The method does not accept any parameters. Return Value:The method returns the string object of the particul 5 min read String Class in Java A string is a sequence of characters. In Java, objects of the String class are immutable, which means they cannot be changed once created. In this article, we are going to learn about the String class in Java.Example of String Class in Java:Java// Java Program to Create a String import java.io.*; cl 7 min read How to Convert a String to an int in Java? In Java, converting a String to an int is done using methods from the Integer class. The methods used for this conversion are Integer.parseInt() and Integer.valueOf(). Example:The Integer.parseInt() is a commonly used method to convert a string to an integer in Java. This method converts a numeric s 1 min read Java Strings In Java, a String is the type of object that can store a sequence of characters enclosed by double quotes, and every character is stored in 16 bits, i.e., using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowi 9 min read Integer toOctalString() Method in Java Octal is the base-8 number system and uses the digits 0 to 7 in operation. Octal is widely used in computing on systems like the PDP-8, and IBM mainframes employed 12-bit, 24-bit or 36-bit words. The Java.lang.Integer.toOctalString() method is a built-in function in Java that is used to return a str 3 min read BigInteger toString() Method in Java BigInteger Class offers 2 methods for toString(). toString(int radix): The java.math.BigInteger.toString(int radix) method returns the decimal String representation of this BigInteger in given radix. Radix parameter decides on which number base (Binary, octal, hex etc) it should return the string. I 3 min read Integer decode() Method in Java It is often seen that any integer within (" ") is also considered as string, then it is needed to decode that into the integer. The main function of java.lang.Integer.decode() method is to decode a String into an Integer. The method also accepts decimal, hexadecimal, and octal numbers. Syntax : publ 2 min read Integer shortValue() Method in Java The Integer.shortValue() is an inbuilt method of java.lang which returns the value of this Integer in the short type . Syntax: public short shortValue() Parameters: The method does not take any parameters. Return Value: The method returns the integer value represented by this object after converting 2 min read StringWriter write(int) method in Java with Examples The write(int) method of StringWriter Class in Java is used to write the specified byte value on the writer. This byte value is specified using the ASCII value of the byte value passed as an integer value. This integer value is taken as a parameter. Syntax: public void write(int ascii) Parameters: T 2 min read Java Convert int to String - Different Ways of Conversion Converting an int to a String is an important type conversion. Many operations can be performed over a string, while we are limited when it comes to integers. We have a wide varied list of in-built methods in the String class that help us perform hassle-free operations. Suppose we are required to co 9 min read Like