-1

I am a beginner, and I would like to know on how do I get this program out of bugs-


public class Calculator
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("*******************************************");
        System.out.println("MC MR MS M+ M-");
        System.out.println("<- CE C  +- √");
        System.out.println("7  8  9  /  %");
        System.out.println("4  5  6  * 1/x");
        System.out.println("1  2  3  - ");
        System.out.println("   0  .  + ");
        System.out.println("   =     ");
        System.out.println("*******************************************");
        System.out.println("");
        boolean stop = false;
        do {
            System.out.println("Please type the number you want to operate upon:");
            double x = sc.nextDouble();
            System.out.println("Please type the number you want to use to operate:");
            double y = sc.nextDouble();
            System.out.println("Type the operators. Available operators:\n1. +\n2. -\n3. *\n4. /\n5. %\n6. ^");
            char ch = sc.next().charAt(0);
            switch(ch) {
                case '+':
                double a = x + y;
                System.out.println("Result of adding the two numbers: " + a);
                break;
            
                case '-':
                double s = x - y;
                System.out.println("Result of subtracting two numbers: " + s);
                break;
            
                case '*':
                double m = x * y;
                System.out.println("Result of multiplying two numbers: " + m);
                break;
            
                case '/':
                double d = x / y;
                System.out.println("Result of dividing two numbers: " + d);
                break;
            
                case '%':
                double mod = x % y;
                System.out.println("Result of the remainder when dividing two numbers: " + mod);
                break;
            
                case '^':
                double p = Math.pow(x,y);
                System.out.println("Result of squaring the number: " + p);
                break;
            
                default:
                System.out.println("Invalid operator.");
                break;
            }
            System.out.println("Continue? Type Y to continue or N to end: ");
            String st = sc.nextLine();
            if(st.equals("n")) {
                stop = true;
            }
            else {
                stop = false;
            }
        } while(!stop);
  }
}

There are no errors at all, these are my wrong-doings in the program. After all the calculations are done, it puts me through a loop, and I don't seem to quite figure it out, on how to get the user input. It comes back to the start.

This is all I can put up, since I really don't have much to tell, if anything, I will edit this questions as users ask questions.

Thanks:)

2 Answers2

1

Replace String st = sc.nextLine() by String st = sc.next().

At this point the scanner has a line break in its buffer (remaining from reading the operator).

nextLine() returns whatever is left in the buffer, it does not wait for additional user input.

By calling next() instead you tell the scanner that you want to read another token. The line break is less than a token, so Scanner waits for additional user input.

Stefan
  • 1,645
  • 1
  • 10
  • 15
-1

You should put a nextLine(); after every nextFoo(); to consume the new line character

Also change to sc.nextLine().charAt(0); as suggested in the comments

import java.util.Scanner;

public class Calculator
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("*******************************************");
        System.out.println("MC MR MS M+ M-");
        System.out.println("<- CE C  +- √");
        System.out.println("7  8  9  /  %");
        System.out.println("4  5  6  * 1/x");
        System.out.println("1  2  3  - ");
        System.out.println("   0  .  + ");
        System.out.println("   =     ");
        System.out.println("*******************************************");
        System.out.println("");
        boolean stop = false;
        do {
            System.out.println("Please type the number you want to operate upon:");
            double x = sc.nextDouble();
            sc.nextLine();//consume next line character
            System.out.println("Please type the number you want to use to operate:");
            double y = sc.nextDouble();
            sc.nextLine();//consume next line character
            System.out.println("Type the operators. Available operators:\n1. +\n2. -\n3. *\n4. /\n5. %\n6. ^");
            char ch = sc.nextLine().charAt(0);//change
            switch(ch) {
                case '+':
                double a = x + y;
                System.out.println("Result of adding the two numbers: " + a);
                break;

                case '-':
                double s = x - y;
                System.out.println("Result of subtracting two numbers: " + s);
                break;

                case '*':
                double m = x * y;
                System.out.println("Result of multiplying two numbers: " + m);
                break;

                case '/':
                double d = x / y;
                System.out.println("Result of dividing two numbers: " + d);
                break;

                case '%':
                double mod = x % y;
                System.out.println("Result of the remainder when dividing two numbers: " + mod);
                break;

                case '^':
                double p = Math.pow(x,y);
                System.out.println("Result of squaring the number: " + p);
                break;

                default:
                System.out.println("Invalid operator.");
                break;
            }
            // better check
            String st ="";
            do {
                System.out.println("Continue? Type Y to continue or N to end: ");
                st = sc.nextLine();
            }while (!st.equals("N") || !st.equals("Y"));

            if(st.equals("N")) {
                stop = true;
            }
            else if (st.equals("Y")) {
                stop = false;
            }
        } while(!stop);
  }
}

Note that you didn't do a very good check to see if user wants to continue

I just suggest using something like this

More info about your problem

AirlineDog
  • 518
  • 4
  • 18
  • You have added a comment saying "Better check", so is it that you have done it wrong or I would have to check? If I have to check, then I get an infinite loop of "Continue? Type Y to continue or N to end" – ShamThePsycho Jan 11 '21 at 16:55
  • I suggest that you check both inputs from user. For example in your code if he enters 'foo' it will keep running. In my code it will ask for new input until he enters 'Y' or 'N' – AirlineDog Jan 11 '21 at 17:01