# Beginner's calculator, supporting M, D, A, S operations on two numbers

I'm new to programming and I sort of understand the concept of methods but, I am little overwhelmed by this program and want to shorten it using methods to make it easier to read and understand.

import java.util.Scanner;

public class NewCalculator {
public static void main(String[] args) {
String usrInput = "";
String Num1Input = "";
String Num2Input = "";
int Num1Int = 0;
int Num2Int = 0;
int SumOfTwo = 0;
Scanner scnr = new Scanner(System.in);
Scanner Num1 = new Scanner(System.in);
Scanner Num2 = new Scanner(System.in);
System.out.println("Please Enter (M)ultiplication, (D)ivision, (A)ddition, or (S)ubtraction");
usrInput = scnr.next();
System.out.println(usrInput);
if (usrInput.equals("M")|| usrInput.equals("m")||
usrInput.equals("D")|| usrInput.equals("d")||
usrInput.equals("A")|| usrInput.equals("a")||
usrInput.equals("S")|| usrInput.equals("s")
)
{ //If Bracket
switch (usrInput) {//Switch Bracket
case "M":
Num1Input = Num1.next();
Num2Input = Num2.next();
Num1Int = Integer.parseInt(Num1Input);
Num2Int = Integer.parseInt(Num2Input);
SumOfTwo = Num1Int * Num2Int;
System.out.println("The Answser to " + Num1Input + " * " + Num2Input + " Equals " + SumOfTwo);
break;
case "m":
Num1Input = Num1.next();
Num2Input = Num2.next();
Num1Int = Integer.parseInt(Num1Input);
Num2Int = Integer.parseInt(Num2Input);
SumOfTwo = Num1Int * Num2Int;
System.out.println("The Answser to " + Num1Input + " * " + Num2Input + " Equals " + SumOfTwo);
break;
case "D":
Num1Input = Num1.next();
Num2Input = Num2.next();
Num1Int = Integer.parseInt(Num1Input);
Num2Int = Integer.parseInt(Num2Input);
SumOfTwo = Num1Int / Num2Int;
System.out.println("The Answser to " + Num1Input + " / " + Num2Input + " Equals " + SumOfTwo);
break;
case "d":
Num1Input = Num1.next();
Num2Input = Num2.next();
Num1Int = Integer.parseInt(Num1Input);
Num2Int = Integer.parseInt(Num2Input);
SumOfTwo = Num1Int / Num2Int;
System.out.println("The Answser to " + Num1Input + " / " + Num2Input + " Equals " + SumOfTwo);
break;
case "A":
Num1Input = Num1.next();
Num2Input = Num2.next();
Num1Int = Integer.parseInt(Num1Input);
Num2Int = Integer.parseInt(Num2Input);
SumOfTwo = Num1Int + Num2Int;
System.out.println("The Answser to " + Num1Input + " + " + Num2Input + " Equals " + SumOfTwo);
break;
case "a":
Num1Input = Num1.next();
Num2Input = Num2.next();
Num1Int = Integer.parseInt(Num1Input);
Num2Int = Integer.parseInt(Num2Input);
SumOfTwo = Num1Int + Num2Int;
System.out.println("The Answser to " + Num1Input + " + " + Num2Input + " Equals " + SumOfTwo);
break;
case "S":
Num1Input = Num1.next();
Num2Input = Num2.next();
Num1Int = Integer.parseInt(Num1Input);
Num2Int = Integer.parseInt(Num2Input);
SumOfTwo = Num1Int - Num2Int;
System.out.println("The Answser to " + Num1Input + " - " + Num2Input + " Equals " + SumOfTwo);
break;
case "s":
Num1Input = Num1.next();
Num2Input = Num2.next();
Num1Int = Integer.parseInt(Num1Input);
Num2Int = Integer.parseInt(Num2Input);
SumOfTwo = Num1Int - Num2Int;
System.out.println("The Answser to " + Num1Input + " - " + Num2Input + " Equals " + SumOfTwo);
break;
default:
System.out.println("We are in the case");
break;
}}else{
System.out.println("You may only enter 'M', 'D', 'A', or 'S'");

}

}//Main Bracket
}//Class Bracket

• Your title should give the purpose of the code and that should be explained in more detail in your description
– user33306
Nov 7, 2018 at 2:07

Actually breaking your code into methods is the least of your problems.

You can re-use the same Scanner for each input.

The Scanner class has a nextInt(). Using this does away with the strings to store the input.

The default: case in the switch block handles any erroneous input, the conditional to check for that is superfluous.

Each case: block contains the same code. You can ask for the numbers first then the operation. This way you only write it once.

case: statements can be stacked to handle the same code for different case:'s.

Storing the numbers in an array allows you to pass them by reference to a method.

The PrintStream class(System.out) has a Format method which works well for concatenating the string output of different objects.

I've put all this together as well showed what can be done with a couple of methods:

public static void main(String[] args) {
Integer[] nums = {0,0,0};
var in = new Scanner(System.in);
getNums( in, System.out,nums);
getResult(in, System.out, nums);
}
public static void getResult(Scanner in, PrintStream out,Integer[] nums){
var goodInput = false;
while (!goodInput) {
goodInput = true;
System.out.println("Please Enter (M)ultiplication, (D)ivision, (A)ddition, or (S)ubtraction");
var usrInput = in.next();
switch (usrInput) {
case "M":
case "m":
nums[2] = nums[0] * nums[1];
printResult(out, nums, '*');
break;

case "D":
case "d":
nums[2] = nums[0] / nums[1];
printResult(out, nums, '/');
break;

case "A":
case "a":
nums[2] = nums[0] + nums[1];
printResult(out, nums, '+');
break;
case "S":
case "s":
nums[2] = nums[0] - nums[1];
printResult(out, nums, '-');
break;
default:
out.println("You may only enter 'M', 'D', 'A', or 'S'");
goodInput = false;
}
}
}
public static void getNums(Scanner in, PrintStream out,Integer[] nums) {
nums[0] = in.nextInt();
nums[1] = in.nextInt();
}

public static void printResult(PrintStream out, Integer[] nums, char operand) {
out.format("The Answser to %1$d %2$c %3$d = %4$d\n", nums[0], operand, nums[1], nums[2]);
}


This is just one of many ways this could be done.