Results 1 to 3 of 3
 01072014, 07:31 AM #1Señ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++) { PEMDAS.add(input[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); } }
PLEASE NOTE  input can be any real number. Supports adding, subtracting, multiplying, and dividing, using +  * / respectively.
Thanks for all of your comments in advance!
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
Finally, add 2 + 12, then add 14 + decimal
Display as a double.Last edited by AlexGraal; 01072014 at 07:36 AM.
 01072014, 08:56 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,785
 Blog Entries
 7
 Rep Power
 21
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,
Joscenosillicaphobia: the fear for an empty beer glass
 01072014, 06:31 PM #3Señor Member
 Join Date
 Jan 2014
 Posts
 184
 Rep Power
 0
Similar Threads

My program has to run in less than a minute. Can anyone help make my code efficient?
By jamgor in forum New To JavaReplies: 14Last Post: 06132013, 07:19 PM 
Finished the code but don't know how to run this...
By gomdohri in forum New To JavaReplies: 5Last Post: 09082011, 04:36 PM 
Need some help with code. Make it more efficient.
By Meta in forum New To JavaReplies: 1Last Post: 03222010, 09:21 AM 
How to run a code when a download is finished
By aneesahamedaa in forum New To JavaReplies: 4Last Post: 10142008, 01:37 PM 
How to write efficient maintainable code.
By Zosden in forum Advanced JavaReplies: 9Last Post: 05012008, 05:48 AM
Bookmarks