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..)
Code:
import java.util.Scanner;
import java.io.FileOutputStream;
public class Calculator {
public void Addition(){
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");
System.out.println("1)Addition\n 2)Subtraction");
choice = scan.nextInt();
switch(choice){
case 1:
Addition();
break;
case 2:
Subtraction();
break;
default:
System.out.println("You did not enter a valid choice.");
}
}
}
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.
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");
System.out.println(" 1)Addition\n 2)Subtraction");
choice = scan.nextInt();
switch(choice){
case 1:
Addition();
break;
case 2:
Subtraction();
break;
default:
System.out.println("You did not enter a valid choice.");
}
}
public void Addition(){
Scanner scan = new Scanner(System.in);
int one, two;
System.out.println("Addiction 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("Answer: ");
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("Answer: ");
System.out.println(one + " - " + two + " = " + (one-two));
}
public static void main(String[] args) {
new Calculator();
}
}
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.
Re: Problem with simple addition and subtraction calculator
Quote:
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.
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
Re: Problem with simple addition and subtraction calculator
Quote:
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 ;-)
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?
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();
}
}
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
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 ==
Re: Problem with simple addition and subtraction calculator
Quote:
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