Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*; // for StringTokenizer, Scanner
- public class EasyCalc {
- public static void main(String[] args) {
- /**
- * Program: EasyCalc.java
- * Creator: Chris Clarke
- * Created: 11.01.2011
- * Purpose: Perform simple calculation by evaluating string.
- * Notes: The text entered can include spaces.
- * For "times", you can use "*" or "x".
- */
- Scanner scan = new Scanner(System.in);
- // declare variable 'theString' to be of type String
- String theString = "";
- // 'length' is an Integer variable
- int length;
- // 'temp' is a String variable
- String temp;
- // num1, num2 and answer are all declared as type double
- double num1 = 0;
- double num2 = 0;
- double answer = 0;
- // 'first', 'operand' and 'second' are declared to be of type String.
- // 'operand' is given an arbitrary value
- String first = "";
- String operand = "&";
- String second = "";
- // declare Boolean flag 'error'
- boolean error;
- do {
- // 'theString' is the text the user types in
- System.out.print("Type your sum: ");
- theString = scan.nextLine();
- if (theString.equals("" )) {
- // user didn't type anything so quit the program
- System.exit(0);
- }
- // assign the variable 'length' the length of string 'theString'
- length = theString.length();
- // get last character of theString and assign it to temp
- temp = theString.substring(length-1, length);
- if (temp.equals("=" )) {
- // remove equals sign at the end, if there was one.
- theString = theString.substring(0, length-1);
- }
- StringTokenizer t = new StringTokenizer(theString, "+-*x/");
- // separate theString into two numbers, separated by the operand
- first = t.nextToken();
- try {
- second = t.nextToken();
- error = false;
- }
- catch (Exception e) {
- // there wasn't a second token because it didn't find...
- // ...an operand "+, -, * or x, /"
- System.out.print("Operand error! Use +, -, * or x, and /");
- System.out.println();
- error = true;
- }
- // end of try/catch statement
- }
- while (error);
- // convert strings to doubles, i.e. floating point numbers
- try {
- num1 = Double.parseDouble(first);
- num2 = Double.parseDouble(second);
- } catch (NumberFormatException e) {
- System.out.print("Number error!");
- System.exit(1);
- }
- // end of try/catch statement
- // is the first letter a minus sign?
- String firstLetter;
- firstLetter = theString.substring(0, 1);
- // is the first number negative?
- if (firstLetter.equals("-" )) {
- num1 *= -1;
- }
- // find which operand was used
- for (int i = 1; i < length; i++) {
- operand = theString.substring(i, i+1);
- if (operand.equals("+" ) || operand.equals("-" )
- || operand.equals("*" ) || operand.equals("x" )
- || operand.equals("/" )) {
- break;
- }
- // end if
- }
- // end for
- // compute answer
- if (operand.equals("+" )) {
- answer = num1+num2;
- }
- else if (operand.equals("-")) {
- answer = num1-num2;
- }
- else if (operand.equals("*") || operand.equals("x")) {
- answer = num1*num2;
- }
- else if (operand.equals("/")) {
- if (num2 == 0) {
- System.out.print("Division by zero error!");
- System.exit(0);
- }
- answer = num1/num2;
- }
- // end if
- // display original sum and the answer
- System.out.println(num1+" "+operand+" "+num2+" = "+answer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement