Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Calculating Strings?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculating Strings?

    I'm working on an algorithm solving program, and I've ran into a slight problem, basically the outputted strings look something like this:
    Equation = (1+1-2)+(2+2)
    String 1 = 1+1-2
    String 2 = 2+2

    It basically cuts out the problems inside the brackets, and places them in a list(which will be used later to replace the problems with answers), so now I need to know how I can turn a string into a integer, and be able to use the symbols that put them together. Someone mentioned I should restructure the strings to look like this:
    1 1 +
    But I don't know how that could work.

    Anyway, I need the strings turned into integers, and the signs readable so that I can read and check how these integers will be combined.

    Thanks!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,504
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Calculating Strings?

    I was the one to advise you in your other post.

    Search the web for info about postfix. You should find a algorithm to do it. Why should you change to postfix? Because it makes evaluation so much easier. 1 + 2 * 3 becomes 1 2 3 * +. Then you read the postfix expression. Each time you read an operand you push it onto a Stack. Each time you read an operator you pop 2 values off the Stack, perform operation and push result back onto Stack.

    Read 1
    Push onto Stack
    Read 2
    Push onto Stack
    Read 3
    Push onto Stack
    Read *
    Pop 3
    Pop 2
    Multiply
    Push 6 onto Stack
    Read +
    Pop 6
    Pop 1
    Add
    Push 7 onto Stack

    Now you have finished reading the postfix expression and the result is the only value left on the Stack: 7.

    If this is too much for you then I suggest attempting something simpler.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Strings?

    Quote Originally Posted by Junky View Post
    I was the one to advise you in your other post.

    Search the web for info about postfix. You should find a algorithm to do it. Why should you change to postfix? Because it makes evaluation so much easier. 1 + 2 * 3 becomes 1 2 3 * +. Then you read the postfix expression. Each time you read an operand you push it onto a Stack. Each time you read an operator you pop 2 values off the Stack, perform operation and push result back onto Stack.

    Read 1
    Push onto Stack
    Read 2
    Push onto Stack
    Read 3
    Push onto Stack
    Read *
    Pop 3
    Pop 2
    Multiply
    Push 6 onto Stack
    Read +
    Pop 6
    Pop 1
    Add
    Push 7 onto Stack

    Now you have finished reading the postfix expression and the result is the only value left on the Stack: 7.

    If this is too much for you then I suggest attempting something simpler.

    Oh it's not, I've done harder. Trust me haha. Thanks though, I just didn't know what a postfix was and didn't bother to google it. I tend to do ignorant things like that when I'm really busy. But, thanks, you were a big help, really haha. I'm gonna keep at this, I'm waaaayyyy too stubborn to give up. Go big or go home haha.

Similar Threads

  1. Calculating average of an array
    By Vika in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 29th, 2011, 08:06 AM
  2. Calculating Price with Command Line - Please Help!
    By rjdelight in forum Object Oriented Programming
    Replies: 3
    Last Post: February 6th, 2011, 03:49 PM
  3. Calculating Business days in java
    By narranil2 in forum Algorithms & Recursion
    Replies: 2
    Last Post: August 17th, 2010, 12:08 PM
  4. question on calculating
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 9th, 2010, 05:06 PM
  5. KB/s download speed calculating
    By Kok in forum Java Theory & Questions
    Replies: 2
    Last Post: December 16th, 2009, 02:05 PM