As we discussed, the main functionality of the parseInt() is to get the integer from the string. The string parameter is parsed by the parseInt() method into a signed decimal number.
We first add the java.lang.integer package that is required for the parseInt() method. After that, we establish the “ParseIntCase1” class where the main() method is defined. Here, we declare the two string variables – “str1” and “str2”. The “str1” variable is initialized with the positive signed string value and the “str2” variable is assigned with the negative signed string value. Both these string variables are then passed as an argument in the parseInt(). The parseInt() method is defined inside the integer objects “num1” and “num2”. After parsing the specified strings to the integers, the print command of Java displays the outcomes of the parseInt() method.
At the time of execution, the parseInt() method of Java generates the signed decimal integer representation of the argument’s integer value.
The parseInt() method is demonstrated in the previous example which takes the single string parameter. The parseInt() method also accepts the second parameter which is known as radix. The radix denotes the value as a numerical base. The parseInt() method parses the string argument into the integer object in radix which is the second argument.
The Java class “ParseIntCase2” is constructed where we have the program’s main() method block. Here, we declare a variable “x” which is called with the parseInt() method. The parseInt() method takes the string as a numeric value along with the radix value. Then, we create another variable “y” which is also deployed with the parseInt() method. Here, the parseInt() method takes the positive signed number as a string and the radix value parameter. After that, we define again a variable “z” which is assigned with the parseInt() method. The parseInt() method accepts the negative signed number string this time and the radix as an argument. Lastly, all the results of the parseInt() method for different integer variables are printed by the system.out.println() method.
The signed decimal integers are returned as the outcome which is based on the given radix integer.
The parseInt() method parses the string. The parsed string to the integer is then used with the binary operator to see the results.
The Java class “parseIntCase3” is implemented first. Then, we have the main() method where the string variable “MyString” is declared. The “MyString” variable is set with the string numeric value. After that, we define another variable “i” of type integer. Here, we assign the parseInt() method which inputs the string variable “MyString”. The parseInt() method converts the specified string into an integer. Next, we have the print command where we concatenate the string object “Mystring” with the integer value using the plus operator. We also use the plus operator with the integer object “i”. Note the different results on the compilation of the program.
The first obtained outcome is string concatenation because the string object contains the string value. Next, the outcome is the sum of the assigned integer and the integer which we get from the parseInt() method.
The parseInt() method also encounters the NumberFormatException error when the conditions are satisfied. The parseInt() method raises the NumberFormatException in the cases which are listed in the following:
The main() method is defined within the Java class “ParseIntCase4”. Here, we create the “positiveNumberCase” objects and the “NegativeNumberCase”. The parseInt() method is deployed and only accepts the string value. Then, we declare two more variables, “StringCase” and the “RadixCase”, where the parseInt() method accepts both the string value and the radix value.
Next, we use the worst case which throws the NumberFormatException. Firstly, we initialize the empty string and then pass it into the parseInt() method. Then, we have a variable “b” where the out-of-range integer is assigned to the parseInt() method. The variable “b” is also assigned with the parseInt() method which accepts the domain of the number system.
As expected, the program compilation throws the NumberFormatException as follows for the given reasons.
The NumberFormatException can be overcome when the parseInt() method is used in the try-catch block. The following example is the user-defined case where the user can enter the value and get the equivalent results:
The two modules of Java are required for this program which we added in the header section. Then, we built a Java class “ParseIntCase5” and constructed the main() method there. Inside the blocks of the main() method, we have a try-catch block. First, we have a try block implementation where the value is taken from the user and store that value in the “input” object of the scanner class. Next, we define a string object “str” that reads the user-defined value. Then, we input the object “str” in the parseInt() method to parse the string value which is user defined into the integer and print it on the console.
We compile the previous code three times to see the different results. The first and the second outcome generate the valid input value. The third outcome is not a parsable string, that’s why the error statement is shown.
The Java parseInt() method is utilized for the typed conversion to convert a string value to an integer. The parseInt() method is inputted with the string value and the radix which then returns the string of the integer primitive type. The parseInt() method returns the exception if the string parameter cannot be transformed into an integer. We explored the parseInt() method syntax and functionality with the different examples in this article.