Results 1 to 7 of 7
  1. #1
    marksey07 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default Simple question...I hope

    Java Code:
    public class Calculator2 {
    	public static void main(String[] args){
    		double result = result_of("17+28-11");
    		System.out.println(result);
    	}
    
    	public static double result_of(String equation)
    	{
    		if(equation.contains("+"))
    		{
    			//equation.split not String.split
    			String[] split_up_equation = equation.split("[+]");
    			double result = 0.0;
    			for(int i=0;i<split_up_equation.length;i++)
    			{
    				//add the double value
    				result += result_of(split_up_equation[i]);
    			}
    			return result;
    		}
    		if(equation.contains("-"))
    		{
    			String[] split_up_equation = equation.split("[-]");
    			double result = 0.0;
    			for(int i=0;i<split_up_equation.length;i++)
    			{
    				result += result_of(split_up_equation[i]);
    			}
    			return result;
    		}
    		return Double.valueOf(equation);
    	}
    }
    This is my code for a calculator, as you can see the method for"-" adds rather than takes away. I don't believe there is a -= rather than +=

    Can anyone help me

  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

    Quote Originally Posted by marksey07 View Post
    This is my code for a calculator, as you can see the method for"-" adds rather than takes away. I don't believe there is a -= rather than +=
    Yup, there's a -= operator for numbers: a-= b subtracts the value b from the value a and assigns it to a again, no problem.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by marksey07 View Post
    I don't believe there is a -= rather than +=
    What happened when you tried? Keep in mind that you don't have to use the += operator; it's just a shortcut for doing something like this:

    Java Code:
    int x = 7;
    x = x + 2; // same as x += 2;
    PS- In the future, you might want to use a more meaningful post title. People tend to ignore posts with generic titles like this one.

    Edit- Too slow! But recommended reading: http://download.oracle.com/javase/tu...bolts/op1.html
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    marksey07 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    so
    Java Code:
    if(equation.contains("-"))
    		{
    			String[] split_up_equation = equation.split("[-]");
    			double result = 0.0;
    			for(int i=0;i<split_up_equation.length;i++)
    			{
    				result -= result_of(split_up_equation[i]);
    			}
    			return result;
    		}
    		return Double.valueOf(equation);
    and
    Java Code:
    double result = result_of("50-20");
    should return 30?

  5. #5
    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

    Quote Originally Posted by marksey07 View Post
    Java Code:
    double result = result_of("50-20");
    should return 30?
    I don't know what your result_of( ... ) method does but if it is even half sane I'd expect that result yes. What's keeping you from trying the code, don't take my word for it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by marksey07 View Post
    so
    Java Code:
    if(equation.contains("-"))
    		{
    			String[] split_up_equation = equation.split("[-]");
    			double result = 0.0;
    			for(int i=0;i<split_up_equation.length;i++)
    			{
    				result -= result_of(split_up_equation[i]);
    			}
    			return result;
    		}
    		return Double.valueOf(equation);
    and
    Java Code:
    double result = result_of("50-20");
    should return 30?
    Again- what happened when you tried it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    marksey07 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    OK, didn't work. But im just going to have a play, I have a good feeling I'm going to get there :) :eek:

    Thanks guys

Similar Threads

  1. very simple Question
    By arsenal4ever_11 in forum NetBeans
    Replies: 2
    Last Post: 05-27-2010, 09:51 PM
  2. hope to help me
    By ABUALI in forum Advanced Java
    Replies: 7
    Last Post: 05-27-2010, 05:43 PM
  3. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 06:32 PM
  4. hope there is an easier way.
    By IYIaster in forum New To Java
    Replies: 6
    Last Post: 10-14-2009, 08:26 AM
  5. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 08:33 PM

Posting Permissions

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