Search code examples
javaswitch-statementcalculator

Java Calculator Not Working - Need Help Troubleshooting ( Computer Science Beginner )


I've written a simple calculator in Java that takes user input for two numbers and an operator. It's supposed to perform the corresponding mathematical operation and output the result. However, my code isn't functioning as expected. I'm having issues with inputting the operator and correctly processing the operations using the switch statement. I would appreciate some help troubleshooting.

public class Main {

    public static void main(String[] args) {


        System.out.println("Willkommen Beim Taschenrechner!!");
        System.out.println("Bitte gibt die 1.te Zahl ein:");

        double zahl1 = new java.util.Scanner( System.in).nextDouble();

        System.out.println("Bitte gib den Operator ( + , - , * , / ) ein:");
        char operator = new java.util.Scanner( System.in).nextLine().charart();

        System.out.println(" Biite gib nun die 2.te Zahl ein:");
        double zahl2 = new java.util.Scanner( System.in ).nextDouble();

        switch ( operator){

            case '+' :
                System.out.println( zahl1 + zahl2);
                break;
            case '-' :
                System.out.println( zahl1 - zahl2);
                break;
            case '*' :
                System.out.println( zahl1 * zahl2);
                break;
            case '/' :
                System.out.println( zahl1 / zahl2);
                break;



        }


    }
}

Solution

  •         char operator = new java.util.Scanner( System.in).nextLine().charart();
    
    

    Incorrect syntax

            char operator = new java.util.Scanner( System.in).nextLine().charAt(0);