Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Incorrect syntax

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading