# Thread: Finished Code - More Efficient Way?

1. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Finished Code - More Efficient Way?

Hello everyone - decided to write a quick calculator that would follow the order of operation rules. Here is the finished script.

My question to you all (the experienced coders) - is there a more efficient way of doing anything in the code? It seems bulky for what I'm trying to do.
Java Code:
```import java.util.Scanner;
import java.util.ArrayList;
class Calculator2 {
public static void main (String[] args) {

Scanner scan = new Scanner( System.in );
double fint = 0;
ArrayList<String> PEMDAS = new ArrayList<String>();

System.out.println("Enter what you want to do");
String[] input = scan.nextLine().split("[ ]+");

for(int z = 0; z < input.length; z++) {
}
if(input.length % 2 != 1) {
System.out.println("Incorrect Entry");
return;
}
double temp = 0;
for(int z = 0; z < PEMDAS.size() - 1; z++) {

if(PEMDAS.get(z).equals("*")) {
temp = Double.parseDouble(PEMDAS.get(z - 1)) * Double.parseDouble(PEMDAS.get(z + 1));
PEMDAS.set(z - 1,Double.toString(temp));
PEMDAS.remove(z);
PEMDAS.remove(z);
}
else if(PEMDAS.get(z).equals("/")) {
temp = Double.parseDouble(PEMDAS.get(z - 1)) / Double.parseDouble(PEMDAS.get(z + 1));
PEMDAS.set(z - 1,Double.toString(temp));
PEMDAS.remove(z);
PEMDAS.remove(z);
}
}

fint = Double.parseDouble(PEMDAS.get(0));
for(int i = 1; i < PEMDAS.size(); i+=2 )  {
if(i != PEMDAS.size()) {
switch(PEMDAS.get(i).charAt(0)) {
case ('-'): fint -= Double.parseDouble(PEMDAS.get(i + 1)); break;
case ('+'): fint += Double.parseDouble(PEMDAS.get(i + 1)); break;
case ('*'): fint *= Double.parseDouble(PEMDAS.get(i + 1)); break;
case ('/'): fint /= Double.parseDouble(PEMDAS.get(i + 1)); break;
default: fint = fint; System.out.println("Called");
}
}
}
if((int) fint == fint)
System.out.println("Result: " + (int) fint);
else
System.out.println("Result: " + fint);
}
}```
It works - I've checked.

PLEASE NOTE - input can be any real number. Supports adding, subtracting, multiplying, and dividing, using + - * / respectively.

EDIT: To clarify in advance what is going on: input is taken, tokenized. Tokens are scanned for * or /. If found, do that operation with surrounding numbers, delete those numbers and replace the sign with the result of those numbers. Then numbers are - and + in the switch statement. If double doesn't end with .0000000, it is displayed as double. Otherwise as int.

Example:
Input: 2 + 3 * 4 - 6 / 7
Tokenize the input to get string array = {2,+,3 . . . 7}
Make that an arraylist for easier manipulation
Check for * or / in list - found between 3 and 4. Replace the * with product of 3 and 4, delete 3 and 4
Remaining: 2 + 12 - 6 / 7
Next: 2 + 12 - .95654 or whatever 6/7 is
Display as a double.
Last edited by AlexGraal; 01-07-2014 at 06:36 AM.

2. ## Re: Finished Code - More Efficient Way?

What about incorrect expressions? e.g. if I type "* * *" your program dies a horrible death ... b.t.w. lines #43 an #44 are not needed because you've already done the multiplication or division ...

kind regards,

Jos

3. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: Finished Code - More Efficient Way?

Yup, I could add in plenty of other checks. I'm assuming the user would enter things correctly.

Is there a better way of getting the info to the arraylist? Can I tokenize directly into an arraylist?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•