We have frequently discussed the method int Integer.parseInt (String s). This me
ID: 3825600 • Letter: W
Question
We have frequently discussed the method int Integer.parseInt (String s). This method will accept a String of characters that represents a decimal integer value and returns this value as an int value. This assignment asks you to write the method code (the method will be called parseInt but you will call it without the Integer. prefix) so that you can get a deeper understanding of what the method is actually doing. The requirements of the method are as follows: 1. If the String is the value nul1 or is an empty String (has length 0) then throw a NumberFormatException by using the statement: throw new NumberFormatException(); as the block of an if statement. This statement will terminate the method and allow the calling method to handle the situation. (In our case we will add the phrase throws NumberFormatException after the header of the main () method and before the opening brace ({) of the method's body. This will cause the program to crash and report the exception to the user.) 2. The String may start with a leading + or - character. If it does then capture the information by setting the int variable sign to -1 if the - is present or + 1 if the + is present or if no sign character is present at all. If there is a valid sign character then it should be removed from the string leaving only decimal digits. 3. You should establish an int variable named value which is initialized to 0. This variable will keep track of the developing value. 4. The remaining characters should be decimal digits. For each character in this string check if the character is a decimal digit. If it is not then throw an exception (see note 1 above) which will end the method and cause the program to crash. 5. For valid digit characters, multiply the variable value by 10 and add the digit's decimal value to the value variable. 6. When all of the digit characters have been converted into the value variable you should multiply value by sign, return this value, and the method is finished. Test the method by calling your method with various strings. Remember to test negative values as well as positive values and also 0. You should also try strings that will cause an exception to be thrown. For reference you may check the Javadoc page for int Integer.parseInt (String s).Explanation / Answer
public class MyStringToNumber {
public static int parseInt(String str) {
if (str == null || str.length() == 0 || "".equals(str)) {
throw new NumberFormatException();
}
int i = 0, number = 0;
boolean isNegative = false;
boolean isValid = true;
boolean signGiven = false;
int len = str.length();
if (str.charAt(0) == '-') {
signGiven = true;
isNegative = true;
i = 1;
} else if (str.charAt(0) == '+') {
signGiven = true;
isNegative = false;
i = 1;
}
while (i < len) {
if (Character.isLetter(str.charAt(i))) {
isValid = false;
break;
}
i++;
}
if (!isValid) {
throw new NumberFormatException();
}
i = signGiven ? 1 : 0;
while (i < len) {
number *= 10;
number += (str.charAt(i++) - '0');
}
if (isNegative)
number = -number;
return number;
}
public static void main(String args[]) throws NumberFormatException {
System.out.println(""3256" == " + parseInt("3256"));
System.out.println(""76289" == " + parseInt("-76289"));
System.out.println(""90087" == " + parseInt("90D087"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.