0

I have a problem. In class we have to do a simple calculator, and my problem is that I wanna write a number, then the operator, then again a number. Somehow my code doesn't work. I can enter the first number but then my program closes :/ Why is that? Is it because I used the data type string?

Thanks to everyone in advance!!

Here is my code:

import java.util.Scanner; 
import java.math.*;
public class Calculatrice 
{
    public static void main(String args[]) 
    {
        double num1;
        Scanner keyb = new Scanner(System.in);
        System.out.println("Calculette Simple");
        System.out.print("Valeur actuelle: ");
        num1 = keyb.nextDouble();

        System.out.print("Entrez un operateur: ");
        String i;
        i = keyb.nextLine();
        double result = 0;

        switch (i)
        {
            case "+":
            result = result + num1;
            break;
            case "-":
            result = result - num1;
            break;
            case "*":
            result = result * num1;
            break;
            case "/":
            result = result / num1;
            break;
            case "sqrt":
            result = Math.sqrt(result);
            break;
            case "c":
            result = 0;
            break;
            case "x":
            System.exit(0);
            break;
            case "^":
            result = Math.pow(result,num1);
            break;
            default:
            System.out.println("Valeurs acceptees: +, -, *, /, ^, sqrt, c, x");
            break;

        }
        keyb.close();
}
    }
reedy420
  • 1
  • 1
  • You've written "but then my program closes" - does your console output an error message of any kind? This would be useful in getting to the root of your problem – Oozeerally Jan 30 '20 at 10:34
  • It closes because there is no repetition (a loop) in your code. – Mark Rotteveel Jan 30 '20 at 16:34
  • 1
    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) – schnaader Jan 30 '20 at 17:01

3 Answers3

0

You need a loop: you need to read data from command line until a condition is verified, in order to read more then one string (number, operator or whatever you want). Then try something like this :

// your initialization of scanner
i = keyb.nextLine();
double result = 0;
while (!i.equals("end")) { // I use this as exit condition, but you can use whatever you want
    switch (i) {
        case "+":
            result = result + num1;
            break;
        case "-":
            result = result - num1;
            break;
        case "*":
            result = result * num1;
            break;
        case "/":
            result = result / num1;
            break;
        case "sqrt":
            result = Math.sqrt(result);
            break;
        case "c":
            result = 0;
            break;
        case "x":
            System.exit(0);
            break;
        case "^":
            result = Math.pow(result, num1);
        default:
            System.out.println("Valeurs acceptees: +, -, *, /, ^, sqrt, c, x");

    }
}
// close scanner

Renato
  • 1,725
  • 8
  • 18
0

you need some loop with exit condition (I belive it's 'x' in input) something like

while (!"x".equals(i)) {
   switch (i)
   ...
}
Roman Golov
  • 356
  • 2
  • 6
0

The program takes the value of both the numbers (entered by user) and then user is asked to enter the operation (+, -, * and /), based on the input program performs the selected operation on the entered numbers using switch case.

import java.util.Scanner;

public class JavaExample {

    public static void main(String[] args) {

        double num1, num2;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter first number:");

        /* We are using data type double so that user
         * can enter integer as well as floating point
         * value
         */
        num1 = scanner.nextDouble();
        System.out.print("Enter second number:");
        num2 = scanner.nextDouble();

        System.out.print("Enter an operator (+, -, *, /): ");
        char operator = scanner.next().charAt(0);

        scanner.close();
        double output;

        switch(operator)
        {
            case '+':
                output = num1 + num2;
                break;

            case '-':
                output = num1 - num2;
                break;

            case '*':
                output = num1 * num2;
                break;

            case '/':
                output = num1 / num2;
                break;

            /* If user enters any other operator or char apart from
             * +, -, * and /, then display an error message to user
             * 
             */
            default:
                System.out.printf("You have entered wrong operator");
                return;
        }

        System.out.println(num1+" "+operator+" "+num2+": "+output);
    }
}
salsinga
  • 1,693
  • 1
  • 10
  • 23