Question

I have this basic program that works pretty well when entering single digit numbers. But when calculating an expression with multiple digits, like 1337 - 456 + 32, the program doesn't move on...it acts likes I did nothing. It doesn't freeze or output an error message, it just stops.

here's the code:

import java.io.*;
import java.util.*;
public class Tester {
    public static void main(String args[]) {
        Scanner kb = new Scanner(System.in);
        System.out.print("Enter number: ");
        String s = kb.nextLine();
        Scanner sc = new Scanner(s);
        //Set delimiters to a plus sign surrounded by any amount of white space...or...
        // a minus sign surrounded by any amount of white space.
        sc.useDelimiter("\\s*");
        int sum = 0;
        int temp = 0;
        int intbefore = 0;
        
        if (sc.hasNext("\\-")) {
            sc.next();
            if (sc.hasNextInt()) {
                intbefore = sc.nextInt();
                int temper = intbefore * 2;
                intbefore = intbefore - temper; 
            }
        }
        if (sc.hasNextInt()) {
            intbefore = sc.nextInt(); //now its at the sign (intbefore = 5)
        }
        sum = intbefore;
        
        while (sc.hasNext()) {
            if(sc.hasNext("\\+")) { //does it have a plus sign?
                sc.next(); //if yes, move on (now at the number)
                System.out.println("got to the next();");
                
                if(sc.hasNextInt()) { //if there's a number
                    temp = sc.nextInt();
                    sum = sum + temp; //add it by the sum (0) and the sum of (5) and (4)
                    System.out.println("added " + sum);
                }
            }
            if(sc.hasNext("\\-")) {
                sc.next();
                System.out.println("got to the next();");
                
                if (sc.hasNextInt()) {
                    temp = sc.nextInt();
                    sum = sum - temp; //intbefore - temp == 11
                    System.out.println("intbefore: " + intbefore + "  temp: " + temp);
                    System.out.println("subtracted " + sum); // subtracted by 11
                }
            }
        }
        System.out.println("Sum is: " + sum);
    }
}

Help with why this happens and what to do to fix it? (I'm using netbeans if that helps)

Also, I'm assuming that the input has a space between each number Ex: 123 + -23 - 5

Was it helpful?

Solution 2

Try to change the dilimeter as "\\s+" in the first place or just use the default one

OTHER TIPS

I tried to execute your code and this is what i found.

Suppose your input was 12+1.

Not sc.hasNextInt() will give you 1 which you are assigning to intbefore.

Next the while loop in your code is going to infinite loop cos sc.hasNext() will always be true (and returning 2 in this case ) and it doesn't match the two if conditions inside the while loop thus making your code run in infinite loop. Now go ahead and work on that. All the best.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top