Results 1 to 3 of 3
  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default 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);
      }
    }
    It works - I've checked.

    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; 01-07-2014 at 07:36 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default 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?

Similar Threads

  1. Replies: 14
    Last Post: 06-13-2013, 07:19 PM
  2. Finished the code but don't know how to run this...
    By gomdohri in forum New To Java
    Replies: 5
    Last Post: 09-08-2011, 04:36 PM
  3. Replies: 1
    Last Post: 03-22-2010, 09:21 AM
  4. How to run a code when a download is finished
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 10-14-2008, 01:37 PM
  5. How to write efficient maintainable code.
    By Zosden in forum Advanced Java
    Replies: 9
    Last Post: 05-01-2008, 05:48 AM

Posting Permissions

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