user7154735
user7154735

Reputation:

Trying to Create a Calculator in Java and Got These Errors: How Do I Fix Them?

I wrote this code in Java to create a very simple calculator.

import java.util.Scanner;

public class Addition {

static void Addition() {
    Scanner numberOne = new Scanner(System.in);
    float x = numberOne.nextFloat();
    System.out.println("First Number: " + numberOne.nextLine());
    Scanner numberTwo = new Scanner(System.in);
    float y = numberTwo.nextFloat();
    System.out.println("Second Number: " + numberTwo.nextLine());
    float sum = x + y;
    System.out.println(sum);    
    }
}

public class Subtraction {

static void Subtraction() {
    Scanner numberOne = new Scanner(System.in);
    float x = numberOne.nextFloat();
    System.out.println("First Number: " + numberOne.nextLine());
    Scanner numberTwo = new Scanner(System.in);
    float y = numberTwo.nextFloat();
    System.out.println("Second Number: " + numberTwo.nextLine());
    float difference = x - y;
    System.out.println(difference); 
    }
}

public class Multiplication {

static void Multiplication() {
    Scanner numberOne = new Scanner(System.in);
    float x = numberOne.nextFloat();
    System.out.println("First Number: " + numberOne.nextLine());
    Scanner numberTwo = new Scanner(System.in);
    float y = numberTwo.nextFloat();
    System.out.println("Second Number: " + numberTwo.nextLine());
    float product = x + y;
    System.out.println(product);    
    }
}

public class Division {

static void Addition() {
    Scanner numberOne = new Scanner(System.in);
    float x = numberOne.nextFloat();
    System.out.println("First Number: " + numberOne.nextLine());
    Scanner numberTwo = new Scanner(System.in);
    float y = numberTwo.nextFloat();
    System.out.println("Second Number: " + numberTwo.nextLine());
    float quotient = x + y;
    System.out.println(quotient);   
}
}

public class Calculate {
    public static void main(String[] args) {
    System.out.println("Calculator");
    System.out.println("Choose an operation:");
    System.out.println("Addition");
    System.out.println("Subtraction");
    System.out.println("Multiplication");
    System.out.println("Division");
    Scanner input = new Scanner(System.in);
    String choice = input.nextLine();
    if(choice.equals("Addition") {
        Addition();
    }
    else if(choice.equals("Subtraction") {
        Subtraction();
    }
    else if(choice.equals("Mutliplication") {
        Mutliplication();
    }
    else if(choice.equals("Division"){
        Division();
    }
    else {
        System.out.println("That wasn't a valid input. Please try again.");
    }
}
}

However, when I tried to run it, I got this error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Syntax error on token ")", ) expected after this token
The method Addition() is undefined for the type Calculate
Syntax error on token ")", ) expected after this token
The method Subtraction() is undefined for the type Calculate
Syntax error on token ")", ) expected after this token
The method Mutliplication() is undefined for the type Calculate
Syntax error on token ")", ) expected after this token
The method Division() is undefined for the type Calculate

at Calculate.main(Calculate.java:14)

I'm a beginner in Java and I'm not quite sure what the error message means. Can someone explain to me what it means and how I do fix it?

Upvotes: 2

Views: 147

Answers (5)

Carl Tiede
Carl Tiede

Reputation: 1

You are placing the methods like addition and subtraction in their own classes so Main doesn't know how to call them.

I would say scrap the public classes preceding each math method. Also change your

System.out.println("Second Number: " + numberTwo.nextLine());

to something like:

System.out.println("Second Number: " + y);

If you want it to actually spit out the respective numbers.

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

You've got a few issues in your code.

Firstly, you don't need a separate class for each method. Just put all the methods together in the same class. That way, you won't need to specify a class name when you call each method.

Secondly, you're missing some ) characters in your if statements. Make sure that each ( character has a matching ). For example, if (choice.equals("Addition")) {.

Thirdly, your multiplication and division methods actually seem to be doing addition. Use * to multiply two numbers, and / to divide them.

Fourth, lose some of those calls to nextLine() and just print the value you've already retrieved. So, for example, System.out.println("First Number: " + numberOne.nextLine()); should be System.out.println("First Number: " + x); and similarly many times in your code.

Upvotes: 1

Rahul Chowdhury
Rahul Chowdhury

Reputation: 681

You had a syntax error where you didn't add the necessary close parenthesis for the if statements.

if(choice.equals("Addition") <---- missing parenthesis {
    Addition();
}

Solution

if(choice.equals("Addition")) {
    Addition();
}
else if(choice.equals("Subtraction")) {
    Subtraction();
}
else if(choice.equals("Mutliplication")) {
    Mutliplication();
}
else if(choice.equals("Division")){
    Division();
}
else {
    System.out.println("That wasn't a valid input. Please try again.");
}

Upvotes: 0

Garrett Nix
Garrett Nix

Reputation: 80

In your Calculate class body, you aren't properly closing your if-statement headers.

if(choice.equals("Addition") {

should be

if(choice.equals("Addition")) { // notice the second closing parenthesee

You have to put a closing parenthesee for every open one you have.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201437

You have defined your Addition method in your class named Addition.

  1. import static Addition.Addtion();

  2. Change Addition() to Addition.Addition().

You then need to do the same for your other methods. You also are missing a ) on each of your choise tests.

if(choice.equals("Addition")) { // <-- count the open and close parens.

Upvotes: 1

Related Questions