Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Simple Calculator

  1. #1
    Junior Member
    Join Date
    Oct 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Calculator

    Hello,
    i am a absolut beginner at java but i have already installed Eclipse and wrote "Hello World". And sorry i m still learning english. Maybe you can say me whats wrong with my code. The compiler say
    Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "operator" is null
    at HelloWorld.main(HelloWorld.java:33) when i type in the "string". Thanks for the answers.

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Locale;
    import java.util.Scanner;

    import HelloWorld.Scan;
    public class HelloWorld {


    public class Scan {

    public static String nextLine() {
    // TODO Automatisch generierter Methodenstub
    return null;
    }

    }

    public static void main(String[] args) {
    Scanner ScannerVariable = new Scanner(System.in);
    System.out.println("Hello World");
    int zahl1, zahl2, ergebnis=0;


    System.out.println("Erste Zahl bitte.");
    zahl1=(int)ScannerVariable.nextInt();
    System.out.println("Zweite Zahl bitte.");
    zahl2=(int) ScannerVariable.nextInt();
    System.out.println("Operator eingeben");
    String operator = Scan.nextLine();


    switch(operator)
    {
    case "+": ergebnis=zahl1*zahl2; break;
    case "-": ergebnis=zahl1/zahl2; break;
    case "*": ergebnis=zahl1+zahl2; break;
    case "/": ergebnis=zahl1+zahl2; break;
    default: System.out.println("Falsche eingabe"); break;
    }

    System.out.println("Das Ergebnis ist: ");
    System.out.println(ergebnis);

    }
    }

  2. #2
    Junior Member
    Join Date
    Sep 2022
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Calculator

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Locale;
    import java.util.Scanner;
     
    import HelloWorld.Scan;
    public class HelloWorld {
     
     
    public class Scan {
     
    public static String nextLine() {
    // TODO Automatisch generierter Methodenstub
    return null;
    }
     
    }
     
    public static void main(String[] args) {
    Scanner ScannerVariable = new Scanner(System.in);
    System.out.println("Hello World");
    int zahl1, zahl2, ergebnis=0;
     
     
    System.out.println("Erste Zahl bitte.");
    zahl1=(int)ScannerVariable.nextInt();
    System.out.println("Zweite Zahl bitte.");
    zahl2=(int) ScannerVariable.nextInt();
    System.out.println("Operator eingeben");
    String operator = Scan.nextLine();
     
     
    switch(operator)
    {
    case "+": ergebnis=zahl1*zahl2; break;
    case "-": ergebnis=zahl1/zahl2; break;
    case "*": ergebnis=zahl1+zahl2; break;
    case "/": ergebnis=zahl1+zahl2; break;
    default: System.out.println("Falsche eingabe"); break;
    }
     
    System.out.println("Das Ergebnis ist: ");
    System.out.println(ergebnis);
     
    }
    }

Similar Threads

  1. Simple Calculator HELP
    By krowley in forum What's Wrong With My Code?
    Replies: 18
    Last Post: April 20th, 2014, 08:55 PM
  2. Help with simple calculator
    By GMPoison in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 22nd, 2013, 01:29 PM
  3. Simple Calculator
    By Spanky13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 25th, 2013, 05:33 PM
  4. Need help building a simple calculator
    By zigma in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 14th, 2011, 01:13 AM
  5. Simple Calculator
    By JKDSurfer in forum Loops & Control Statements
    Replies: 3
    Last Post: June 28th, 2011, 01:37 PM