# Still learning, looking for general advice about a 4-function calculator I programmed using java

here's some code I wrote in java SE to get use to if statements and scanning. just looking for suggestions that will improve this program or/and teach me techniques that I should get use to using.

import java.util.Scanner;

public class Calculator {
public static void main(String[] args) {
System.out.print("Enter the first value ");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
System.out.print("Enter the next value ");
int y = scan.nextInt();
System.out.println("Choose an operation: 1-multiply 2-divide 3-add 4-subtract");
int z = scan.nextInt();
if (z == 1) {
int a = x * y;
System.out.println("Result: " + a);
}//end of if multiply
else if (z == 2) {
int a = x / y;
System.out.println("Result: " + a);
}//end of if divide
else if (z == 3) {
int a = x + y;
System.out.println("Result: " + a);
else {
int a = x-y;
System.out.println("Result: " + a);
} //end of is subtract
}// End of main
}// End of public class


here's my new program, still not perfect but the class i'm taken hasn't even gone over data types and classes yet so i'm already ahead lol Thank you every one for your help!

import java.util.Scanner;

public class Calculatortwo {
public static void main(String[] args) {
//get imput values from user
System.out.print("Enter the first value ");
Scanner scan = new Scanner(System.in);
double x = scan.nextDouble();
System.out.print("Enter the next value ");
double y = scan.nextDouble();
System.out.println("Choose an operation: 1-multiply 2-divide 3-add 4-subtract");
int operation = scan.nextInt();
//calculator logic
double result = 0;
switch (operation) {
case 1:
result = x * y;
break;
case 2:
if(y != 0) {
result = x / y;
}
else {
System.out.println("Error: Divide by 0");
}
break;
case 3:
result = x + y;
break;
case 4:
result = x - y;
break;
default:
}
System.out.println("Result: " + result);
}
}

• Welcome to CodeReview@SE. While waiting for reviews/"answers", you may want to (re)visit How do I ask a Good Question?: the title deserves improvement. Check to get use to if statements, and if you scroll to the end of the code block, you may notice that in contrast to, say, SO, CR "closing code fences" still need a newline to follow it. – greybeard Jan 20 at 6:58

## Input validation

The input validation is missing. If the user enters a letter, the program fails with InputMismatchException.

An approach is to catch the exception, output an error message, and then either exit or ask the user to try again.

Few other cases to cover:

• For y=0, the division will throw an ArithmeticException, so a second validation is needed in case of division.

• The choice of the operation can only be an integer between 1 and 4 inclusive.

• An operation on two big integers can overflow the integer result.

if (z == 1) {
...
}//end of if multiply
else if (z == 2) {
...
}//end of if divide
else if (z == 3) {
...
else {
...
} //end of is subtract


Commenting // end after each clause is unnecessary since it is clear that the clause ends with a }. If the comments are there to remember that z==1 means multiplication, then it's a sign that the code isn't clear enough. You can use constants or enums to encode the meaning of a value. For example:

private static final int MULTIPLICATION = 1;
//..
if (z == MULTIPLICATION) {
...
}


## Extract method

As @TedBrownlow pointed out, the logic of the calculator can be extracted into its own method. This makes the code more readable and easier to test. By easier to test I mean that a method like:

public class Calculator {
public static int calculate(int x, int y, int operand) {
//...
}
}


Can be easily tested in a unit test like:

public class CalculatorTest {
@Test
public void testMultiplication() {
assertEquals(4, Calculator.calculate(2,2,1));
}
}


## Duplicated code

Printing the result is duplicated four times. It can be avoided by declaring a outside of the chain of if-else and then printing only at the end. From:

if (z == 1) {
int a = x * y;
System.out.println("Result: " + a);
}//end of if multiply
else if (z == 2) {
int a = x / y;
System.out.println("Result: " + a);
}//end of if divide


To:

int a = 0;
if (z == 1) {
a = x * y;
}
else if (z == 2) {
a = x / y;
}
//...
System.out.println("Result: " + a);


## Naming

A better name for a and z can be result and choice.

## Accuracy

The division of two integers doesn't necessarily produce an integer. Consider providing better accuracy or inform the user that it is an integer division.

It would be best to codify the various operations in a separate function. This limits the opportunity for error and eliminates the redundancy of the System.out.println("Result: " + a); statements.

   private static int applyOperation(int x, int y, int op) {
switch(op) {
case 1: return x*y;
case 2: return x/y;
case 3: return x+y;
case 4: default: return x-y;
}
}
public static void main(String[] args) {
System.out.print("Enter the first value ");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
System.out.print("Enter the next value ");
int y = scan.nextInt();
System.out.println("Choose an operation: 1-multiply 2-divide 3-add 4-subtract");
int op = scan.nextInt();
int ans = applyOperation((x), y, op);
System.out.println("Result: " + ans);
}