MCMaricopaAZ

Java Calculator with InteliJ

Nov 28th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. package me.scott.assignment;
  2.  
  3. import java.util.Scanner;
  4. /*
  5. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  6. Training: Create a simple 4 operation calculator in JAVA
  7. Date: 2019-11-27
  8. Author: Scott Hammontree
  9. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  10. */
  11.  
  12. public class Calculator {
  13.  
  14. public static void main(String[] args) {
  15. Calculator calc = new Calculator();
  16. calc.listen();
  17. }
  18.  
  19.  
  20. private void listen() {
  21. System.out.println("Simple Calculator is active. \n" +
  22. "Please enter a simple equation using a whole number.\n" +
  23. "with one of the following operators [+|-|*|/]\n" +
  24. "e.g.[ 12 + 2 | 12 - 2 | 12 * 2 | 9 / 3]\n" +
  25. "e.g. number operator number with spaces included\n");
  26. Scanner scanner = new Scanner(System.in);
  27.  
  28. while (true) {
  29.  
  30. // asking for user input
  31. System.out.println("Waiting for input....");
  32.  
  33. // variable to store the command.
  34. String command = "";
  35.  
  36. // wait for user input
  37. command = scanner.nextLine();
  38.  
  39. // Leave on exit
  40. if (command.equals("exit") || command.toUpperCase().equals("EXIT")) {
  41. break;
  42. } else {
  43.  
  44. // Put the equation elements into a string array [0,1,2]
  45. // Splits this string around matches of the given regular expression i.e. " ".
  46. String[] args = command.split(" ");
  47.  
  48. if (args.length != 3) {
  49. System.out.println("Invalid syntax. \n" +
  50. "Correct syntax is [number operator number] including spaces" +
  51. "e.g. -> 1 + 2");
  52.  
  53. // 'continue' is unnecessary as the last statement in a loop.
  54. // I use continue to return to the top of the loop and skip further processing.
  55. continue;
  56. }
  57.  
  58. // Check the 1st and 3rd string arg. IsNumeric()?
  59. double firstNumber, secondNumber;
  60. try {
  61. firstNumber = Double.parseDouble(args[0]);
  62. secondNumber = Double.parseDouble(args[2]);
  63. } catch (NumberFormatException ex) {
  64. System.out.println("Please enter a whole numbers eg: [3 | 4 | 12].");
  65. System.out.println("ERROR: one of the arguments is not a valid number ");
  66. System.out.println("ERROR: " + ex);
  67. continue;
  68. }
  69.  
  70. // CHECK for DIVIDE by ZERO
  71. if (secondNumber == 0) {
  72. System.out.println("Cannot DIVIDE by ZERO!!");
  73. continue;
  74. }
  75.  
  76. // Check the operators and calculate appropriate result.
  77. switch (args[1]) {
  78. case "+":
  79. System.out.println("Result: " + (firstNumber + secondNumber));
  80. break;
  81. case "-":
  82. System.out.println("Result: " + (firstNumber - secondNumber));
  83. break;
  84. case "*":
  85. System.out.println("Result: " + (firstNumber * secondNumber));
  86. break;
  87. case "/":
  88. System.out.println("Result: " + (firstNumber / secondNumber));
  89. break;
  90. default:
  91. System.out.println("Operator not recognized!");
  92. break;
  93. } // END SWITCH
  94.  
  95. } // END IF
  96.  
  97. } // END of scanner loop
  98.  
  99. System.out.println("Calculator is shutting down.\nGood bye.");
  100. } // END of listen
  101.  
  102. } // END of class
Advertisement
Add Comment
Please, Sign In to add comment