Advertisement
brilliant_moves

SimpleCalculator.java

Dec 21st, 2013
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.40 KB | None | 0 0
  1. import java.util.*;     // for StringTokenizer, Scanner
  2.  
  3. public class SimpleCalculator {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         String theString = "";
  8.         int length;
  9.         // num1, num2 and answer are declared as type double
  10.         double num1 = 0;
  11.         double num2 = 0;
  12.         double answer = 0;
  13.  
  14.         String first = "";
  15.         String operator = "";
  16.         String second = "";
  17.  
  18.         boolean error;
  19.         do {
  20.             // 'theString' is the text the user types in
  21.             System.out.print("Type your sum (q = quit): ");
  22.             theString = scan.nextLine();
  23.             if (theString.equals("q")) {
  24.                 // quit the program
  25.                 System.exit(0);
  26.             } // end if
  27.             // assign the variable 'length' to length of String 'theString'
  28.             length = theString.length();
  29.  
  30.             StringTokenizer t = new StringTokenizer(theString, "+-*/%");
  31.             // separate theString into two numbers, separated by the operator
  32.                 first = t.nextToken();
  33.             try {
  34.                 second = t.nextToken();
  35.                 error = false;
  36.             }
  37.             catch (Exception e) {
  38.                 // there wasn't a second token because it didn't find an operator
  39.                 System.out.print("Error! Use the format: <number>+-*/%<number>");
  40.                 System.out.println();
  41.                 error = true;
  42.             } // end try/catch
  43.         } while (error);
  44.  
  45.         // convert strings to doubles
  46.         num1 = Double.parseDouble(first);
  47.         num2 = Double.parseDouble(second);
  48.         // is the first letter a minus sign?
  49.         String firstLetter;
  50.         firstLetter = theString.substring(0, 1);
  51.         // is the first number negative?
  52.         if (firstLetter.equals("-" )) {
  53.             num1 *= -1;
  54.         } // end if
  55.  
  56.         // find which operator was used
  57.         for (int i = 1; i < length; i++) {
  58.             operator = theString.substring(i, i+1);
  59.             if (operator.equals("+" ) || operator.equals("-" ) || operator.equals("*" ) ||
  60.              operator.equals("/" )) {
  61.                 break;
  62.             } // end if
  63.         } // end for
  64.  
  65.         // compute answer
  66.         switch(operator) {
  67.             case "+" :  answer = num1+num2; break;
  68.             case "-" :  answer = num1-num2; break;
  69.             case "*" :  answer = num1*num2; break;
  70.             case "/"if (num2 == 0) {
  71.                         System.out.print("Division by zero error!");
  72.                         System.exit(0);
  73.                     } // end if
  74.                     answer = num1/num2; break;
  75.             case "%" :  answer = num1%num2; break;
  76.             default  :  System.out.print("Wrong operator.");
  77.                     System.exit(0);
  78.         } // end switch
  79.  
  80.         // display original sum and answer
  81.         System.out.print(num1+operator+num2+"="+answer);
  82.  
  83.     } // end main()
  84. } // end class SimpleCalculator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement