Hussein Farah
Hussein Farah

Reputation: 89

Can't locate Java substring in for loop

I'm trying to make a calculator app and I have a string for the final equation inputted. "983+388+12" How I'm solving is by first identifying where the operator is. After this I loop backwards and forwards trying to find the 2 numbers and then I add them. So I would find the plus symbol then 983 and 388 and add them. I'm having trouble matching the previous/next numbers in relation to the add symbol.

public static void main(String[] args)
{
    int plus=0;
    String string="983+388+12";

    //locating Plus symbol
    for(int i=0;i<string.length();i++)
    {
        if(string.charAt(i)=='+')
        {
            plus=i;
        }
    }

    //locating next number (should be 388)
    for(int i=plus+1;i<string.length();i++)
    {
        if(string.charAt(i)=='+' || string.charAt(i)=='-')
        {
            String nextNumber=string.substring(plus+1, i-1);
            System.out.println(nextNumber);
            break;
        }
    }

I am not getting anything returned as a value for nextNumber

Upvotes: 0

Views: 77

Answers (3)

geco17
geco17

Reputation: 5294

Try using only one loop, see below (working example). I didn't break anything up into smaller methods.

public static void main(String[] args) {
    String string="983+388+12";
    String numberStr="0";
    int lastSignPos = 0;
    int signPos=0;
    char sign = '+';
    int total=0;

    //locating Plus symbol
    for(int i=0; i < string.length(); i++) {
        if(string.charAt(i)=='+' || string.charAt(i)=='-') {
            lastSignPos = signPos;
            signPos = i;
            numberStr = "0";

            if (lastSignPos == 0){
                // first number in series
                numberStr = string.substring(0,signPos);
            } else {
                numberStr = string.substring(lastSignPos + 1, signPos); 
            }

            sign = '+';
            if (string.charAt(lastSignPos)=='-'){
                sign = '-';
            }

            total += Integer.parseInt(sign + numberStr);
        }
    }

    // take care last number
    numberStr = string.substring(signPos+1);
    sign = '+';
    if (string.charAt(signPos)=='-'){
        sign = '-';
    }

    total += Integer.parseInt(sign + numberStr);

    System.out.println(total);
}

Upvotes: 1

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

You can try something like this :

String example = "123+456+678";
String delim = "+";
StringTokenizer st = new StringTokenizer(example,delim); 
while (st.hasMoreElements()) {
    System.out.println("StringTokenizer Output: " + st.nextElement());
}

Upvotes: 0

rgettman
rgettman

Reputation: 178263

When you find the + in the first loop, you don't stop scanning the string. This results in plus marking the location of the last +, which is after the 388 and before the 12. The second loop never finds a + or a - after the last +, so nothing is ever printed. When you find the first +, break out of the loop.

Also, to avoid just 38 being found, correct your substring call, where the ending index is exclusive.

String nextNumber = string.substring(plus + 1, i);

Upvotes: 5

Related Questions