0

Basically I'm trying to make a java calculator. I think its taking the output as input but I'm not sure. It keep taking some sort of input when I didn't input anything. Anyone have any idea why? The repo can be found at https://github.com/Croticalism/Java-calculators if anyone wants to take a look at the whole thing. I really have no idea why this happening in some places but it doesn't in other.

static void SC() {
        // stores two numbers
        double num1 = 0.0;
        double num2 = 0.0;
        boolean valid = false;
        char op = 0;
        while (!valid) {
            try {
                System.out.println("Enter the first number");
                num1 = sc.nextInt();
                valid = true;
                // catches the mismatch exception so it will only take int input. If letter
                // return Not a number
            } catch (InputMismatchException e) {
                System.out.println("Not a number.");
                sc.next();
            }
        }
        while (!valid) {
            try {
                System.out.println("Enter the second number");
                num2 = sc.nextInt();
                valid = true;
                // catches the mismatch exception so it will only take int input. If letter
                // return Not a number
            } catch (InputMismatchException e) {
                System.out.println("Not a number.");
                sc.next();
            }
        }
        while (!valid) {
            try {
                System.out.println("Enter the operator (+,-,*,/)");
                op = sc.next().charAt(0);
                valid = true;
                // catches the mismatch exception so it will only take int input. If letter
                // return Not a number
            } catch (InputMismatchException e) {
                System.out.println("Not a number.");
                sc.next();
            }
        }

        double o = 0;

        switch (op) {

            // case to add two numbers
            case '+':

                o = num1 + num2;

                break;

            // case to subtract two numbers
            case '-':

                o = num1 - num2;

                break;

            // case to multiply two numbers
            case '*':

                o = num1 * num2;

                break;

            // case to divide two numbers
            case '/':

                o = num1 / num2;

                break;

            default:

                System.out.println("You enter wrong input");

                break;
        }

        System.out.println("The answer is:");

        System.out.println();

        // print the final result
        System.out.println(num1 + " " + op + " " + num2
                + " = " + o);
    }

I have a lot of methods, this is one of the methods that are causing problems ^

System output:

Enter the first number 5 You enter wrong input The answer is:

5.0 0.0 = 0.0

The choice you entered is invalid. Pick one from below

Here is the terminal output that I get ^

  • This is an incomplete example. The rules are that you post a minimal, complete, verifiable example in your post. Linking off site is not acceptable. – Jeff Holt May 26 '22 at 01:59
  • However it would be way too big to post the whole code here. – Croticalism May 26 '22 at 02:02
  • This looks like nice opportunity to read: [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Pshemo May 26 '22 at 02:02
  • BTW you are reading operator via `op = sc.next().charAt(0);` but never validate it. Note that `InputMismatchException` will not be thrown by `next()` method as it can return *any* non-zero-length token so your `try-catch` there makes no sense. You need something else (like good old `if`). – Pshemo May 26 '22 at 02:06
  • @Croticalism The skill of creating a *minimal*, complete, verifiable example is something you cannot live without. If you create such an example, you will solve the problem yourself. – Jeff Holt May 26 '22 at 02:16

0 Answers0