-2
public String addFraction(String x, String y) {
    char[] invalid = {'`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', 'q', 'Q', 'w', 'W', 'e', 'E', 'r', 'R', 't', 'T', 'y', 'Y', 'u', 'U', 'i', 'I', 'o', 'O', 'p', 'P', '{', '[', '}', ']', '|', '\\', 'a', 'A', 's', 'S', 'd', 'D', 'f', 'F', 'g', 'G', 'h', 'H', 'j', 'J', 'k', 'K', 'l', 'L', ':', ';', '\"', '\'', 'z', 'Z', 'x', 'X', 'c', 'C', 'v', 'V', 'b', 'B', 'n', 'N', 'm', 'M', '<', ',', '>', '.', '?'};
    char[] xSet = x.toCharArray();
    char[] ySet = y.toCharArray();  
        for (int counter = 0; counter < xSet.length; counter++) {
            for (int symbol = 0; symbol < invalid.length; symbol++) {
                if (xSet[counter] == invalid[symbol]) {
                    return "";
                }
            }
        }
        for (int counter = 0; counter < ySet.length; counter++) {
            for (int symbol = 0; symbol < invalid.length; symbol++) {
                if (ySet[counter] == invalid[symbol]) {
                    return "";
                }
            }
        }
    if ((x.startsWith("/") || x.endsWith("/") ) || (y.startsWith("/") || y.endsWith("/"))) {
        return "";
    }
    String[] sub = new String[4];
    char[] newSet = new char[2];
    int pointer = 0, ctr = 0;

    for (int counter = 0; counter < xSet.length; counter++) {
        if (xSet[counter] == '/') {
            pointer = 0;
            sub[ctr] = String.valueOf(newSet);
            ctr++;
            ++counter;
            continue;
        }
        newSet[pointer] = xSet[counter];
        pointer++;
    }
    for (int counter = 0; counter < ySet.length; counter++) {
        if (xSet[counter] == '/') {
            pointer = 0;
            sub[ctr] = String.valueOf(newSet);
            ctr++;
            continue;
        }
        newSet[pointer] = xSet[counter];
        ++pointer;
    }
    int[] xInt = new int[2];
    xInt[0] = Integer.parseInt(sub[0]);
    xInt[1] = Integer.parseInt(sub[1]);
    int[] yInt = new int[2];
    yInt[0] = Integer.parseInt(sub[2]);
    yInt[1] = Integer.parseInt(sub[3]);

    int num = xInt[0] + yInt[0];
    String z = Integer.toString(num);
    z = z + "/" + yInt[0];

    return z;
}
ajb
  • 31,309
  • 3
  • 58
  • 84
  • 1
    When your program throws a `NumberFormatException`, it gives you useful information such as the line where the exception occurs. And yet you've chosen to hide this very useful information from us at the same time you ask us to help you. You've also chosen not to tell us what the values of `x` and `y` are. Do you think we can read your screen from here? – ajb Nov 26 '14 at 16:02
  • Could you post the complete stack trace? Furthermore a bit more text besides the header would be nice! – Icewind Nov 26 '14 at 16:02
  • 1
    Please provide more details. NFEs occur when the number cannot be parsed. An empty string, for example, causes an NFE, e.g., http://stackoverflow.com/a/8781000/438992 – Dave Newton Nov 26 '14 at 16:02
  • Wild crystal ball guess: `xSet`/`ySet` confusion in the second loop –  Nov 26 '14 at 16:04
  • You never use `ySet`values. Is that normal? – jhamon Nov 26 '14 at 16:22
  • you have so many invalid symbols, it would be easier and safer to test the valid ones. – jhamon Nov 26 '14 at 16:36

1 Answers1

0

Converting empty character to integer will throw number format exception as it's not a valid integer. Below is explanation for the same.

The reason is your newset array is an empty array with empty value. You defined newset as:

char[] newSet = new char[2];

And then tried to use:

if (xSet[counter] == '/') {
        pointer = 0;
        sub[ctr] = String.valueOf(newSet);
        ctr++;
        ++counter;
        continue;
    }
    newSet[pointer] = xSet[counter];
    pointer++;

Now if ctr is 0 and your character equals "/", you are assigning subctr[0] = ""

And number can't contain any character other than number themselves in case of integer. And once you get newset you try to assign it's value to sub[ctr], you do the following conversion to int from subctr:

sub[ctr] = String.valueOf(newSet);
...
xInt[0] = Integer.parseInt(sub[0]);

Did you meant to call sub[ctr] = String.valueOf(newSet); post newSet[pointer] = xSet[counter];?

SMA
  • 36,381
  • 8
  • 49
  • 73
  • as he previously tested if fis input start whith `/`, he should never get the case where `xSet[counter] == '/'`is true at the beginning of his first loop. – jhamon Nov 26 '14 at 16:21
  • no see his assignment to sub array. sub[ctr] = String.valueOf(newSet); here if ctr is 0, then it will assign empty value to sub[0] which OP tries to convert to integer and hence gets NFE. – SMA Nov 26 '14 at 16:24
  • it will assign `String.valueOf(newSet)` and newSet should have been initialized at least during the previous loop (as the first char of `xSet`is not `/`. – jhamon Nov 26 '14 at 16:27
  • i never mentioned its "/" – SMA Nov 26 '14 at 16:30
  • so if it's not `/`, it will execute `newSet[pointer] = xSet[counter]; pointer++;` and `newSet`won't be "an empty array with empty value" – jhamon Nov 26 '14 at 16:38
  • thanks, almas shaikh. i think i'll try to redo the code to fix the empty index problem. – Lachman Nov 28 '14 at 15:10
  • Sure and let us know how it goes. In the mean time, if this helped close this question by accepting the answer. – SMA Nov 28 '14 at 15:32