0

I want it to loop again when the user enters "Y" or "y" and quit when they enter "N" or "n". The quitting option works, however, when they enter Y/y, it shows the first system out, but does not let the user pick which operation they wish to do. Instead the option to continue pops up again and inhibits the user from making any choice.

Here is the code:

import java.util.Scanner;

public class Calc2 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        double numOne, numTwo, ans;
        String option;

        do {

            System.out.println(
                    "For addition press '1', for subtraction press '2', for division press '3', for multiplication press '4'");
            String choice = input.nextLine();

            if (choice.contains("1")) {
                System.out.println("Enter the first number : ");
                numOne = input.nextDouble();
                System.out.println("Enter the second number : ");
                numTwo = input.nextDouble();

                ans = numOne + numTwo;
                System.out.println("The answer is: " + ans + " ya bish.");
            }

            else if (choice.contains("2")) {
                System.out.println("Enter the first number : ");
                numOne = input.nextDouble();
                System.out.println("Enter the second number : ");
                numTwo = input.nextDouble();

                ans = numOne - numTwo;
                System.out.println("The answer is: " + ans + " ya bish.");
            } else if (choice.contains("4")) {
                System.out.println("Enter the first number : ");
                numOne = input.nextDouble();
                System.out.println("Enter the second number : ");
                numTwo = input.nextDouble();

                ans = numOne * numTwo;
                System.out.println("The answer is: " + ans + " ya bish.");
            } else if (choice.contains("3")) {
                System.out.println("Enter the first number : ");
                numOne = input.nextDouble();
                System.out.println("Enter the second number : ");
                numTwo = input.nextDouble();

                ans = numOne / numTwo;
                System.out.println("The answer is: " + ans + " ya bish.");

            }
            System.out.println("Press 'Y' to continue or 'N' to quit.");

            option = input.next();
        } while (option.equals("Y") || option.equals("y"));

        if (option.equals("N") || option.equals("n")) {
            System.out.println("Thank you. ");
        }

    }
}

If anyone can help me, it'd be greatly appreciated. Thanks!

Ori Lentz
  • 3,600
  • 6
  • 20
  • 28
  • Recommended reading: [**How to debug small programs**](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – durron597 Aug 23 '15 at 05:51
  • Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – TNT Aug 23 '15 at 05:52

3 Answers3

1

Please change below line in your code

    String choice = input.nextLine();

from this code

    String choice = input.next();
Subodh Joshi
  • 10,006
  • 23
  • 84
  • 177
0

There trouble you see here is the use of nextLine after nextDouble. Check here [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Community
  • 1
  • 1
  • only links are not preferred as the links may expire, so also please quote the important part from the link which explains your answer. – Jigar Aug 23 '15 at 06:04
0

Your problem appears to be at the beginning of your do-while loop as such:

System.out.println(
        "For addition press '1', for subtraction press '2', " +
         "for division press '3', for multiplication press '4'");
String choice = input.nextLine();

This is the only place where you use nextLine method (rahter than next or nextDouble and so on). This means that after you've read the option argument at the end of the iteration:

option = input.next();

there's still a new line character that hasn't been read by the scanner. When you do nextLine() in the next iteration it reads the new line character before the user has any chance to input something).

Either change that first line to input.next() as well, or make sure every time you read a value, you clear the new line character (for instance by reading nextLine and then casting the value - this would also allow you to do input validations).

Ori Lentz
  • 3,600
  • 6
  • 20
  • 28