Results 1 to 7 of 7

Thread: Calculator

  1. #1
    Elviii is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Calculator

    Hi there,
    I'm fairly new to Java and was going to program a calculator whereby the input is entered in a single expression (e.g 4 + 3 * 5) rather than asking for the first integer, then an operator, then second integer etc.
    I also want the program to do the math in the correct order such that multiplication is before addition and so on.
    However, i've had a 2-3 week break from programming during the Christmas period so I have just come across a complete mental block of how I would even go about starting this calculator.
    I have read about a scanner class which could be used but I have never used it myself before so would this be useful?
    Any help is greatly apprectiated, thanks :)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Calculator

    I wrote a blog article for this forum on an (infix) expression compiler once; its source code is free of use; (see the link at the right side of this reply).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Elviii is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Calculator

    Thanks, but the sample code at the bottom of the blog no longer works :(

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Calculator

    Quote Originally Posted by Elviii View Post
    Thanks, but the sample code at the bottom of the blog no longer works :(
    Strange, I just gave it a try and it works fine for me ... I'll try to attach the .zip file here ... hm, strange; the attachment facility seems to be broken; I'll see if it can be fixed.

    kind regards,

    Jos
    Last edited by JosAH; 01-05-2013 at 03:41 PM.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Elviii is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Calculator

    I get this message:

    vBulletin Message
    Invalid Attachment specified. If you followed a valid link, please notify the administrator

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Calculator

    That's exactly what I'm going to do ;-) I don't have any facilities to fix it myself (thank god)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Calculator

    You could use the Shunting Yard Algorithm to parse your expression.

    Shunting-yard algorithm - Wikipedia, the free encyclopedia

    To tokenize your expression, you could use Java's StringTokenizer class.

    StringTokenizer tokenizer = StringTokenizer(expression, delimiters, true);

    Your operators could be the delimiters.

    You would then call tokenizer.nextToken() to get each token of the expression.

    Anyway, that would take some work, but it would work.

Similar Threads

  1. Need help with + - * / calculator
    By Aconitum in forum New To Java
    Replies: 26
    Last Post: 08-04-2012, 07:13 PM
  2. RMD Calculator
    By java software in forum Java Software
    Replies: 0
    Last Post: 10-08-2011, 05:42 PM
  3. Help with AWT CALCULATOR
    By Megan Dosnueve in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 05:49 PM
  4. need help with my calculator
    By semoche in forum AWT / Swing
    Replies: 6
    Last Post: 12-04-2009, 10:16 PM
  5. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 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
  •