0

I am trying to make a simple calculator which can add, substract, divide and multiply any two numbers. The way it works is that I ask for a number, then I ask for a sign (+,-,*,/) , and then I ask for the second number. The math should be done accordingly afterwards. However I can't get the 'math sign' to be recognized properly by the program.

Here is my code:

package practice1;
import java.util.Scanner;


public class maiin  {

public static void main(String[] args){

    System.out.println("it works.");
    double num1;
    double num2;
    String sign;
    double total;

    System.out.println("Please enter a number: ");
    Scanner input = new Scanner(System.in);

    num1 = input.nextDouble();


    System.out.println("Enter a +,-,/,* sign: ");

    sign = input.nextLine();
    input.nextLine();


    System.out.println("Enter another number: ");


    num2 = input.nextDouble();



    if (sign.equals("+")){
        total = num1 + num2;
        System.out.println("Your total is: " + total);
    } else if (sign.equals ("-")) {
        total = num1 - num2;
        System.out.println("Your total is: " + total);
    } else if (sign.equals ("/")) {
        total = num1 / num2;
        System.out.println("Your total is: " + total);
    } else if (sign.equals ("*")) {
        total = num1 * num2;
        System.out.println("Your total is: " + total);
    } else {
        System.out.println("Please enter the a proper sign");

    }

When I run the program, I always get "Please enter the a proper sign".

Thank you in advanced.

xasuma
  • 3
  • 2
  • 1
    Have you debugged your code? Maybe your input does not match one of your operators and may have a leading or tailing whitespace. You can try `trim()` on your `sign` variable to avoid such whitespaces. – Claas Wilke Jul 26 '14 at 20:25

1 Answers1

-1

I think you need to change

    sign = input.nextLine();
input.nextLine();

to

    sign = input.nextLine();
sign.nextLine();
Bobdabiulder
  • 162
  • 3
  • 12