1

I need to create a parseInt method from scratch and so far I'm getting it. The only part is I am having trouble figuring out how to find out if a int variable is overflowing or underflowing, and if it is then to throw an IllegalArgumentException in Java.

For better understanding this is my task:

Create static method parseInt(String) that converts a string into an int value, positive or negative. Method throws IllegalArgumentException when...

  1. A string contains non-digit characters other than '-' at (as the very first character of the sting).
  2. A string has only '-' and no digits.
  3. A string represents a number that is too large to be stored as integer and produces overflow
  4. A string represents a number that is too small to be stored as integer and produces underflow

Here is what I have so far

 /**
 * Convert a string into an integer value
 * @param str The string to be converted
 * @return answer
 * @throws IllegalArgumentException if answer is underflowing or overflowing
 */
public static int parseInt(String str) throws IllegalArgumentException {
    int answer = 0, factor = 1;
    int i = 0;
    for (i = str.length()-1; i >= 0; i--) {
        answer += (str.charAt(i) - '0') * factor;
        factor *= 10;
    }

    boolean isNegative = false;
    if(str.charAt(0) == '-') {
        isNegative = true;
    }

    if (answer > Integer.MAX_VALUE) throw new IllegalArgumentException();
    if (answer < Integer.MIN_VALUE) throw new IllegalArgumentException();

    if (isNegative) {
        answer = -answer;
    }

    return answer;
}
  • It is not possible for an int value to be larger than Integer.MAX_VALUE nor less than Integer.MIN_VALUE, because those define the maximum and minimum values that the int can hold. –  May 17 '19 at 00:45
  • @another-dave yes I realized that. But then how could I? – Moses Minchuk May 17 '19 at 00:46
  • Instead of `int answer`, I'd opt for `long answer`, and then simply cast it to an `int` when returning it. – Jacob G. May 17 '19 at 00:46
  • It is true that if a+b < a, for both a and b non-negative, then a+b has overflowed. It is also true that for any non-negative values of a, b, if there is overflow then a+b < 0. You ought to be able to piece together a solution from those facts. –  May 17 '19 at 00:48
  • I am not keen on the solution with 'long answer' since, if I then ask you to produce parseLong, there will not be a simple mapping from your parseInt solution to a parseLong solution (well, I suppose you could use bignums, but still...) –  May 17 '19 at 00:50

1 Answers1

0

This does it without using a long

public static int parseInt(String str) throws IllegalArgumentException {
    int answer = 0, factor = 1;

    boolean isNegative = false;
    if(str.charAt(0) == '-') {
        isNegative = true;
    }

    for (int i = str.length()-1; i >= (isNegative ? 1 : 0); i--) {
        int nextInt = (str.charAt(i) - '0') * factor;

        if(isNegative && answer > Math.abs(Integer.MIN_VALUE + nextInt))
            throw new IllegalArgumentException();
        else if(!isNegative && answer > Integer.MAX_VALUE - nextInt)
            throw new IllegalArgumentException();

        answer += nextInt;
        factor *= 10;
    }

    if (isNegative) {
        answer = -answer;
    }

    return answer;
}
DCTID
  • 1,277
  • 9
  • 13