Ask Question

The 'parseInt' method of the 'Integer' class throws a 'NumberFormatException' when it is passed a String argument that it cannot convert to an int. Given the String variable s (which has already been assigned a value), write the code needed to convert the value in s assigning the result to the integer variable i If a NumberFormatException is thrown, i should be assigned the value - 1.

+1
Answers (1)
  1. 7 August, 06:13
    0
    Following are the code in the java language

    try / / try block

    {

    i = Integer. parseInt (s); / / converting into the integer

    }

    catch (NumberFormatException e) / / catch block

    {

    i = - 1; / / initialized i to - 1.

    }

    Explanation:

    Following is the description of code.

    Create a try block in that clock we convert the variable "s" into the integer by using the function Integer. parseInt and storing in the variable "i". if try block gives an error the console is directly going in the second we create a catch block for that try block In this block we initialized the variable i to - 1.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “The 'parseInt' method of the 'Integer' class throws a 'NumberFormatException' when it is passed a String argument that it cannot convert to ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers