# Thread: Problem with simple addition and subtraction calculator

1. Member Join Date
Feb 2012
Posts
2
Rep Power
0

## Problem with simple addition and subtraction calculator

I just started learning Java and im coming from C++. Can someone explain to me why i cant call Addition() and Subtraction() from main? Also, what can i do to clean this code up?(Make it shorter, easier to read, etc..)

Java Code:
```import java.util.Scanner;
import java.io.FileOutputStream;

public class Calculator {

Scanner scan = new Scanner(System.in);
int one, two;

System.out.println("Enter your first number: ");
one = scan.nextInt();

System.out.println("Enter your second number: ");
two = scan.nextInt();

System.out.println(one + " + " + two + " = " + (one+two));
}

public void Subtraction(){
Scanner scan = new Scanner(System.in);
int one, two;

System.out.println("Enter your first number: ");
one = scan.nextInt();

System.out.println("Enter your second number: ");
two = scan.nextInt();

System.out.println(one + " - " + two + " = " + (one-two));
}

public static void main(String[] args) {
int choice;
Scanner scan = new Scanner(System.in);

System.out.println("CALCULATOR\n\n");
choice = scan.nextInt();

switch(choice){
case 1:
break;
case 2:
Subtraction();
break;
default:
System.out.println("You did not enter a valid choice.");
}
}
}```
Last edited by Nano; 02-05-2012 at 03:19 AM.  Reply With Quote

2. Member Join Date
Feb 2012
Posts
31
Rep Power
0

## Re: Problem with simple addition and subtraction calculator

The reason as to why you were getting errors is because, you were trying to call Addition and Subtraction from non-static methods, into your main method, which is static. Also I have done some editing to your code, to make it slightly more presentable. :) Good luck.
Java Code:
```import java.util.*;
import java.io.*;

public class Calculator {
public Calculator() {
int choice;
Scanner scan = new Scanner(System.in);

System.out.println("JAVA CALCULATOR\n");
choice = scan.nextInt();

switch(choice){
case 1:
break;
case 2:
Subtraction();
break;
default:
System.out.println("You did not enter a valid choice.");
}
}

Scanner scan = new Scanner(System.in);
int one, two;

System.out.println("Enter your first number: ");
one = scan.nextInt();

System.out.println("Enter your second number: ");
two = scan.nextInt();
System.out.println("////////////////////////////");
System.out.println(one + " + " + two + " = " + (one+two));
}

public void Subtraction(){
Scanner scan = new Scanner(System.in);
int one, two;

System.out.println("Subtraction Selected!\n\n");

System.out.println("Enter your first number: ");
one = scan.nextInt();
System.out.println("Enter your second number: ");
two = scan.nextInt();
System.out.println("////////////////////////////");
System.out.println(one + " - " + two + " = " + (one-two));
}

public static void main(String[] args) {
new Calculator();
}
}```
Last edited by Vampiricx3; 02-05-2012 at 03:48 AM.  Reply With Quote

3. Senior Member Join Date
Oct 2011
Posts
106
Rep Power
0

## Re: Problem with simple addition and subtraction calculator

The problem isn;t calling them in your main the method. The problem is that you cannot call them inside a switch statement.  Reply With Quote

4. Member Join Date
Feb 2012
Posts
31
Rep Power
0

## Re: Problem with simple addition and subtraction calculator Originally Posted by mwr1976 The problem isn;t calling them in your main the method. The problem is that you cannot call them inside a switch statement.
By calling them in the switch statement, inside the Calculator() constructor instead of the main class, it works perfectly fine. Of course you can call them inside a switch statement. Also, if you actually went ahead and copied OP's source code, you'd notice the errors.  Reply With Quote

5. ## Re: Problem with simple addition and subtraction calculator

Note that class names should start with an uppercase letter, method names with a lowercase letter. Read about this and more in Code Conventions for the Java Programming Language: Contents

db  Reply With Quote

6. ## Re: Problem with simple addition and subtraction calculator Originally Posted by mwr1976 The problem isn;t calling them in your main the method. The problem is that you cannot call them inside a switch statement.
Unless you add the following comment line next to it:"

// pretty please with sugar on top?

kind regards,

Jos ;-)  Reply With Quote

7. Member Join Date
Feb 2012
Posts
2
Rep Power
0

## Re: Problem with simple addition and subtraction calculator

THank you everyone! I had it working but i wanted to make the code better so i changed it. Now it doesnt work lol. The problem is that it cant read the operation. It always goes to make last else statement. Can anyone tell me what is wrong?

Java Code:
```import java.util.Scanner;
import java.io.FileOutputStream;

public class Calculator {
public Calculator(){
int choice;
int one, two;
String sign;
Scanner scan = new Scanner(System.in);

System.out.println("CALCULATOR\nPress 1 to use.\nPress 0 to exit.");
choice = scan.nextInt();

while(choice != 0){
System.out.println("\n\nFirst number: ");
one = scan.nextInt();
System.out.println("Operation:");
sign = scan.next();
System.out.println("Second number: ");
two = scan.nextInt();

if(sign == "+"){
System.out.println(one + " + " + two + " = " + (one+two));
}
else if(sign == "-"){
System.out.println(one + " - " + two + " = " + (one-two));
}
else if(sign == "*"){
System.out.println(one + " * " + two + " = " + (one*two));
}
else if(sign == "/"){
System.out.println(one + " / " + two + " = " + (one/two));
}
else
System.out.println("You did not enter a valid operation.");

}
}

public static void main(String[] args) {
new Calculator();
}
}```  Reply With Quote

8. ## Re: Problem with simple addition and subtraction calculator

Don't compare Strings for equality with the == operator. Use the equals( ... ) method instead.

kind regards,

Jos  Reply With Quote

9. Member Join Date
Feb 2012
Posts
31
Rep Power
0

## Re: Problem with simple addition and subtraction calculator

Exactly as Jos said, using .equals(""); is a lot more secure in your code then just using ==  Reply With Quote

10. ## Re: Problem with simple addition and subtraction calculator Originally Posted by Vampiricx3 Exactly as Jos said, using .equals(""); is a lot more secure in your code then just using ==
Nothing to do with security. The two do different comparisons.

db  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•