1

I am trying to make a simple calculator program. However, I am also trying to make it so that if the user enters in something that is not a double, it will ask again for a double. However, when I try to enter in something like "e" for a double, it gave me an input.mismatch.exception. I put in a try catch block, and all that does is infinitely asks for input without giving a chance to put input, then errors out because it overloads the RAM giving me a stackoverflow (nice) error. code:

/**
 * @author camper
 * Jul 1, 2019
 * 3:28:04 PM
 */
import java.util.Scanner;

public class io2 {

    static double num1;
    static String op;
    static double num2;

    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        doStuff();

    }

    public static void doStuff() {

        getNum1();
        getOp();
        getNum2();

        calc(num1, op, num2);
    }

    public static void calc(double a, String c, double b) {

        if (c.equals("+")) {
            System.out.println(a + b);
        } else if (c.equals("-")) {
            System.out.println(a - b);
        } else if (c.equals("*")) {
            System.out.println(a * b);
        } else if (c.equals("/")) {
            System.out.println(a / b);
        } else {
            System.out.println("That ain't no operator");
            doStuff();
        }
    }



    public static void getNum1() {
        try {
            System.out.println("Enter your first number:");
            num1 = input.nextDouble();

        }catch (Exception e) {
            getNum1();
        }
    }

    public static void getOp() {
        System.out.println("Enter what operator you want (+,-,*,/):");
        op = input.nextLine();
    }

    public static void getNum2() {
        System.out.println("Enter your second number:");
        num2 = input.nextDouble();

    }

}

  • Don't use `nextDouble()`: use `nextLine()` to get a String, and parse that to a double. Also, don't catch `Exception`, catch `InputMismatchException`. And don't use a recursive call, use a loop. – Andy Turner Jul 03 '19 at 18:53
  • Also possibly related to [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Mansur Gulami Jul 03 '19 at 18:55
  • 1
    And it is **not** causing an infinite **loop**. It creates a **recursion**. A **loop** doesnt lead to a stackoverflow. Only recursion does (because each new method call requires that stack space is allocated ... stack space is limited, thus sooner or later, when you keep calling methods, but not returning from them, you run out of it) – GhostCat Jul 03 '19 at 18:55
  • This is very likely duplicate from . Pretty sure the issue you are having is that you are not calling next and nextDouble throws an exception and therefore will not actually pass the current input. Give it a try calling input.next() before you call getNum1() again. I would definitely take a look at the link I shared and refactor to a while loop instead of recursion though. – Sam Furlong Jul 03 '19 at 18:56

0 Answers0