Java Integer.parseInt() MethodThe Java parseInt() method is a method of Integer class that belong to java.lang package. The parseInt() method in Java is crucial for converting string representations of numbers into actual integer values. This capability is fundamental in various programming scenarios, such as when numerical data comes in text form-through user input, data files, or external systems-and needs to be used for calculations, comparisons, or as part of the application's logic. The conversion process is essential for handling numeric data correctly and efficiently, enabling developers to write more flexible, reliable, and powerful Java applications. By providing easy-to-use tools for working with different data types, Java helps bridge the gap between the often text-heavy world of data input/output and the numeric calculations central to software development. There are the following three variants of parseInt() methods that can be differentiated depending on its parameter.
Purpose of parseInt() MethodThe parseInt() method in Java is used to change numbers that are written as text (strings) into actual numbers (integers) that the computer can understand and work with. For example, if you have the text "149" and you want to add 3 to it, you first need to change "149" into a number. The following Java program demonstrate the same. Filename: ParseIntExample.java Output: 152 By using parseInt(), we can easily turn text into numbers, making it easier to do things like math operations. This method is really helpful when we are dealing with data that comes in as text but we need to use it as numbers, like user input or information from files. Working of parseInt() MethodThe parseInt() method in Java is designed to convert a string into an integer. Here's a step-by-step breakdown of how it works:
This process allows the parseInt() method to accurately convert a string representation of a number into an actual integer value, ready to be used for mathematical operations and other purposes where a numerical value is required. Advantages of the parseInt() MethodThe parseInt() method in Java offers several advantages that make it a useful tool for developers. Here are some of the key benefits:
Syntax of parseInt() MethodFollowing are the declarations of parseInt () method: Parameter:
Returns:
Exceptions:NullPointerException: If s is null. IndexOutOfBoundsException: If beginIndex is negative, or if beginIndex is greater than endIndex or if endIndex is greater than s.length (). NumberFormatException: If the CharSequence does not contain a parsable int in the specified radix, or if radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX. Compatibility Version:Java 1.2 and above:
Java 9:
1. Java Integer parseInt (String s) MethodThis method parses the String argument as a signed decimal integer object. The characters in the string must be decimal digits, except that the first character of the string may be an ASCII minus sign '-' to indicate a negative value or an ASCII plus '+' sign to indicate a positive value. It returns the integer value which is represented by the argument in a decimal integer. Syntax: Filename: ParseIntExample.java Output: The string converted to integer is: 1234 After adding 100 to the converted integer: 1334 2. Java Integer parseInt (String s, int radix) MethodThis method parses the String argument as a signed decimal integer object in the specified radix by the second argument. The characters in the string must be decimal digits of the specified argument except that the first character may be an ASCII minus sign '-' to indicate a negative value or an ASCII plus sign '+' to indicate a positive value. The resulting integer value is to be returned. Syntax: Filename: ParseIntRadixExample.java Output: ParseIntRadixExample 3. Java Integer parseInt (CharSequence s, int beginText, int endText, int radix)This method parses the CharSequence argument as a signed integer in the specified radix argument, beginning at the specified beginIndex and extending to endIndex - 1. This method does not take steps to guard against the CharSequence being mutated while parsing. Syntax: Filename: ParseIntCharSequenceExample.java Output: The parsed integer is: 43981 Example 1Filename: IntegerParseIntExample1.java Test it NowOutput: Value = 20 Value = 20 Value = -20 Example 2Filename: IntegerParseIntExample2.java Test it NowOutput: Value = 104 Value = 512 Value = -484 Example 3Filename: IntegerParseIntExample3.java Test it NowOutput: 200100 300 Example 4Filename: IntegerParseIntExample4.java Test it NowOutput: int i = 100 Example 5Filename: IntegerParseIntExample5.java Test it NowOutput: Exception in thread "main" java.lang.NumberFormatException: For input string: "10A" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at myPackage.IntegerParseIntExample5.main(IntegerParseIntExample5.java:6) Overall, the parseInt() method is a powerful utility for processing and manipulating numeric data within Java applications, offering a combination of flexibility, efficiency, and robustness. Next TopicInteresting-facts-about-null-in-java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India