1

I'm writing a simple calculator in java where you can perform 4 basic operations with only 2 operands. What i need is that inside the if statement I want to perform a loop where a user will continue doing the same operation when ask to continue and if it quits it will be ask again to continue by selecting another operations. My problem is that it is not performing a loop inside the if statements instead it performs a loop where a user is ask to continue by selecting another operations.

import java.util.Scanner;

public class calculator
{





public static void main(String [] args)
{
    Scanner input = new Scanner (System.in);
    double x, y;
    double operation;
    String getChar;
    String chooseOperation;

    do
    {   
        System.out.println("Choose your operation:");
        System.out.println("A. Addition");
        System.out.println("B. Subraction");
        System.out.println("C. Multiplication");
        System.out.println("D. Division");

        chooseOperation = input.nextLine();

        if ((chooseOperation.equals("A")) | (chooseOperation.equals("a")))
        {
            do
            {
                System.out.println("You Choose Addition\n\n\n");
                System.out.println("Input the first number:\n");
                x = input.nextDouble();
                System.out.println("Input the second number:\n");
                y = input.nextDouble();
                operation = x+y;
                System.out.println("The sum is :"+operation);

                System.out.println("Continue?");
                System.out.println("Y/N");
                getChar=input.nextLine();
            }while(getChar.equals("Y"));
        }

        else if ((chooseOperation.equals("B")) | (chooseOperation.equals("b")))
        {
            do
            {
                System.out.println("You Choose Subtraction\n\n\n");
                System.out.println("Input the first number:\n");
                x = input.nextDouble();
                System.out.println("Input the second number:\n");
                y = input.nextDouble();
                operation = x-y;
                System.out.println("The difference is :"+operation);

                System.out.println("Continue?");
                System.out.println("Y/N");
                getChar=input.nextLine();
            }while(getChar.equals("Y"));
        }

        else if ((chooseOperation.equals("C")) | (chooseOperation.equals("c")))
        {
            do
            {
                System.out.println("You Choose Multiplication\n\n\n");
                System.out.println("Input the first number:\n");
                x = input.nextDouble();
                System.out.println("Input the second number:\n");
                y = input.nextDouble();
                operation = x*y;
                System.out.println("The product is :"+operation);

                System.out.println("Continue?");
                System.out.println("Y/N");
                getChar=input.nextLine();
            }while(getChar.equals("Y"));
        }

        else if ((chooseOperation.equals("D")) | (chooseOperation.equals("d")))
        {
            do
            {
                System.out.println("You Choose Division\n\n\n");
                System.out.println("Input the first number:\n");
                x = input.nextDouble();
                System.out.println("Input the second number:\n");
                y = input.nextDouble();
                operation = x/y;
                System.out.println("The quotient is :"+operation);

                System.out.println("Continue?");
                System.out.println("Y/N");
                getChar=input.nextLine();
            }while(getChar.equals("Y"));
        }
        else
        {
            System.out.println("Invalid Selection!\n\n\n");
        }
        System.out.println("Do you want to continue?\n\nY/N");
        getChar=input.nextLine();
    }while((getChar.equals("Y"))|(getChar.equals("y")));

}    
}
5
  • please cut down the code to the part where you have trouble with Commented Sep 18, 2014 at 12:28
  • 1
    What is the input that you are giving when you are testing your program? Do you type Y or y? Commented Sep 18, 2014 at 12:30
  • if your referring to the loop in if statements, it's Y since i'm having trouble giving it a boolean expression which involves bitwise operators Commented Sep 18, 2014 at 12:36
  • (to the questioners) Just run the program with A , 6, 7 and you see the problem. There is something wrong with input.nextLine(). It gets skipped... Commented Sep 18, 2014 at 12:40
  • @OhemgiIstalLes you can refer to the do while loops inside the if...else statements. The computer is suppose to ask you if you want to continue doing the same operations but instead it cancelled the loops Commented Sep 18, 2014 at 12:40

2 Answers 2

1

Read the entire line every time then parse into double:

          //Addition
          do
            {
                System.out.println("You Choose Addition\n\n\n");
                System.out.println("Input the first number:\n");
                x = Double.parseDouble(input.nextLine());//read the whole line, then parse into double
                System.out.println("Input the second number:\n");
                y = Double.parseDouble(input.nextLine());//read the whole line, then parse into double
                operation = x + y;
                System.out.println("The sum is :" + operation);

                System.out.println("Continue?");
                System.out.println("Y/N");
                getChar = input.nextLine();//when not reading the whole line, getChar is empty
            }
            while (getChar.equals("Y"));

Otherwise, getCharseem to contain the next line character created when user input the numbers (type the number then press Enter).

Sign up to request clarification or add additional context in comments.

6 Comments

is there a package in using the double.parseDouble?
it's Double.parseDouble, not double.parseDouble. It's part of the default Java framework java.lang.Double
thanks for your help it work!! can you tell me how does the java.lang.Double functions. i'm still a student by the way with a hearing impaired problem so i'm not that good in listening the advice of my professors. how about showing me a link instead.
here you go. docs.oracle.com/javase/7/docs/api/java/lang/… As ortis wrote, the problem with input.nextDouble(); is that there is still a "new line sign" in the line afterwards so with the next input.nextLine(); you are reading that and not asking the user for a new input.
In addition to the link Robert gave you, you can check out the source code of the Double.parseDouble method but that might be a litte complex for a beginner. grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/…
|
0

Its a long time ago I programmed java. But I think its because you're using only one |. I'm not sure but I thought java needs two |. So your first if will be:

if (chooseOperation.equals("A") || chooseOperation.equals("a"))
{

}

3 Comments

actually I applied that "||" instead of "|" using the same concept i've learned in c++ but i"m getting an error so i only use "|"
In this example, using | or || makes no difference (except, maybe, for execution speed).
The operators | and & are not only bitwise operators (when applied to integer arguments), but also non-short-circuiting logical operators that evaluate both operands regardless of the value of the first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.