1
\$\begingroup\$

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":
            System.out.println("Please enter Number 1");
            Num1Input = Num1.next();
            System.out.println("Please enter Number 2");
            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":
            System.out.println("Please enter Number 1");
            Num1Input = Num1.next();
            System.out.println("Please enter Number 2");
            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":
            System.out.println("Please enter Number 1");
            Num1Input = Num1.next();
            System.out.println("Please enter Number 2");
            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":
            System.out.println("Please enter Number 1");
            Num1Input = Num1.next();
            System.out.println("Please enter Number 2");
            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":
            System.out.println("Please enter Number 1");
            Num1Input = Num1.next();
            System.out.println("Please enter Number 2");
            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":
            System.out.println("Please enter Number 1");
            Num1Input = Num1.next();
            System.out.println("Please enter Number 2");
            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":
            System.out.println("Please enter Number 1");
            Num1Input = Num1.next();
            System.out.println("Please enter Number 2");
            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":
            System.out.println("Please enter Number 1");
            Num1Input = Num1.next();
            System.out.println("Please enter Number 2");
            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
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Your title should give the purpose of the code and that should be explained in more detail in your description \$\endgroup\$
    – user33306
    Nov 7 '18 at 2:07
1
\$\begingroup\$

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) {
    out.println("\nPlease enter the first number:");
    nums[0] = in.nextInt();
    out.println("\nPlease enter the second number:");
    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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.