-1

To summarize, I am attempting to clean my code up. The program is prompting a user to enter (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit - each letter is associated with an if statement. "q" will be the sentinel value exiting the program. User will need to enter two numbers which would then be executed by one of the above operations. My problem is that I cannot seem to solve why "Enter first number" is being prompted alongside "Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?". I am probably over looking the simplest thing. I hope my explanation was clear enough. Also, any suggestions on improving code readability would be greatly appreciated.

The goal was to ask the user how they would like to execute their two numbers. Once selected, they would then enter their two numbers - immediately followed by the sum. Once the sum is presented, they are asked again which operation to choice.

import java.util.Scanner;

public class ScratchCode {
  public static void main(String[] args) {
    
    Scanner input = new Scanner(System.in);
    String operationLetter;
    int firstNumber;
    int secondNumber;

    System.out.println("Hello! Welcome to the world's worst calculator.\n");

    do {
        System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
        operationLetter = input.nextLine();

        System.out.println("Enter first number.");
        firstNumber = input.nextInt();
    
        System.out.println("Enter second number.");
        secondNumber = input.nextInt();
    
        if (operationLetter.equalsIgnoreCase("a")) {
           int sum = firstNumber + secondNumber;
           System.out.println("Great. " +  firstNumber + " + " +  secondNumber + " = " + sum);
        }
    
        else if (operationLetter.equalsIgnoreCase("s")) {
            int sum = firstNumber - secondNumber;
            System.out.println("Great. " +  firstNumber + " - " +  secondNumber + " = " + sum);
        }
    
        else  if (operationLetter.equalsIgnoreCase("m")) {
            int sum = firstNumber * secondNumber;
            System.out.println("Great. " +  firstNumber + " * " +  secondNumber + " = " + sum);
        }
    
        else if (operationLetter.equalsIgnoreCase("d")) {
            int sum = firstNumber / secondNumber;
            System.out.println("Great. " +  firstNumber + " / " +  secondNumber + " = " + sum);
        }
    }
    while(!operationLetter.equalsIgnoreCase("q"));

    System.out.println("Goodbye !");
  }

}
Qiu Zhou
  • 1,070
  • 6
  • 11
Kalkai
  • 11
  • 3
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Martheen Sep 10 '20 at 03:00

3 Answers3

1

Please change the input for your sentinel values as below:

System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
operationLetter = input.next();

The problem was with input.nextLine();

Soni
  • 142
  • 8
  • Brilliant! I figured I was over looking something. Although the program is finished.... for now lol. Would you recommend anything to fix the problem when entering a value outside of what's asked (a,s,m,d,q). The program will move forward instead of looping back when lets say "z" is entered. @soni Kimari – Kalkai Sep 10 '20 at 02:57
  • You can create an inner loop for getting correct operation, doing an eager check for quit and existing the out loop will be to restructure further. – Soni Sep 10 '20 at 03:04
  • Thank you @soni Kumari. I will look into adding this feature moving forward. Much appreciated! – Kalkai Sep 10 '20 at 03:09
0

Not an answer just trying to improve a bit your code, first create a method if you se some code being repeated, second that seems a perfect usage of a switch case, here is my attempt to demonstrate that:

private void bestCalcEver() {
    Scanner input = new Scanner(System.in);
    String operationLetter;
    int firstNumber;
    int secondNumber;

    System.out.println("Hello! Welcome to the world's worst calculator.\n");

    do {
        System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
        operationLetter = input.nextLine();

        System.out.println("Enter first number.");
        firstNumber = input.nextInt();

        System.out.println("Enter second number.");
        secondNumber = input.nextInt();


        switch (operationLetter.toLowerCase()) {
            case "a":
                printResult(firstNumber, secondNumber, firstNumber + secondNumber, "+");
                break;
            case "s":
                printResult(firstNumber, secondNumber, firstNumber - secondNumber, "-");
                break;
            case "m":
                printResult(firstNumber, secondNumber, firstNumber * secondNumber, "*");
                break;
            case "d":
                if (secondNumber == 0) {
                    System.out.println("Oops, I can't divide by 0 sorry =(");
                } else {
                    printResult(firstNumber, secondNumber, firstNumber / secondNumber, "/");
                }
                break;
            case "q":
                System.out.println("Goodbye !");
                break;
            default:
                System.out.println("Invalid operator, sorry");
                break;

        }


    } while(!operationLetter.equalsIgnoreCase("q"));


}

private void printResult(int firstNumber, int secondNumber, int result, String operator) {
    System.out.println("Great. " +  firstNumber + operator +  secondNumber + " = " + result);
}
Luciano Ferruzzi
  • 825
  • 6
  • 18
  • Thank you so much @Luciano Ferruzzi. I didn't even think about using a switch case. I felt as if I was repeating my code a bit, switch case will help this. – Kalkai Sep 10 '20 at 03:10
0

Issues to Solve:

  1. The Buffer needs to cleaned up, it has some unused data that needs to cleaned, due to which call to input.nextLine(); is automatically get's it from buffer, which in your case is not the expected behavior, so need to clean that up.
  2. Second the placement of sentinel is not where it should be, so I changed the loop little bit that way you can compare
  3. Input validation, other minor improvements could be done, which I also skipped

Code:

class ScratchCode {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Hello! Welcome to the world's worst calculator.\n");

        System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
        String operationLetter = input.nextLine();

        while (!operationLetter.equalsIgnoreCase("q")) {
            System.out.println("Enter first number.");
            int firstNumber = input.nextInt();

            System.out.println("Enter second number.");
            int secondNumber = input.nextInt();

            if (operationLetter.equalsIgnoreCase("a")) {
                int sum = firstNumber + secondNumber;
                System.out.println("Great. " + firstNumber + " + " + secondNumber + " = " + sum);
            } else if (operationLetter.equalsIgnoreCase("s")) {
                int sum = firstNumber - secondNumber;
                System.out.println("Great. " + firstNumber + " - " + secondNumber + " = " + sum);
            } else if (operationLetter.equalsIgnoreCase("m")) {
                int sum = firstNumber * secondNumber;
                System.out.println("Great. " + firstNumber + " * " + secondNumber + " = " + sum);
            } else if (operationLetter.equalsIgnoreCase("d")) {
                int sum = firstNumber / secondNumber;
                System.out.println("Great. " + firstNumber + " / " + secondNumber + " = " + sum);
            }

            // Buffer Cleanup
            input.nextLine();

            System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
            operationLetter = input.nextLine();
        }

        System.out.println("Goodbye !");
    }

}
ImtiazeA
  • 1,044
  • 12
  • 17