Results 1 to 6 of 6
  1. #1
    Alpharius120 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Checking if a Boolean Within Another Class Object is True

    So I have a project I'm working on, and for one part, I have to check if a boolean is true. However, this boolean is a local variable in a method of another class type I made, and I don't know how to check for that.

    So here's the method in the non-main class, "Stock":
    Java Code:
    public boolean Buy(int num, double pr)
    	{
    		boolean ok = false;
    		if (pr > 0.95*sprice && pr < 1.05*sprice)
    		{
    			ok = true;
    			svolume -= num;
    			sprice = pr;
    		}
    		return ok;
    	}
    	
    	public boolean Sell(int num, double pr)
    	{
    		boolean ok = false;
    		
    		if (pr > 0.95*sprice && pr < 1.05*sprice)
    		{
    			ok = true;
    			svolume += num;
    			sprice = pr;
    		}
    		return ok;
    	}
    and here's how I'm trying to check, unsuccessfully.

    Java Code:
    choice4 = Integer.parseInt(JOptionPane.showInputDialog("Please enter one of the following numbers to sell that stock:" + stockOut + "\n" + (numberOfStocks + 1) + " to exit"));
    							
    							if(choice4 != (numberOfStocks + 1)) {
    								Boolean testSell = myStocks[choice4 - 1].Sell(numberBought = Integer.parseInt(JOptionPane.showInputDialog("How many of the " + myStocks[choice4 - 1].getName() + " stocks would you like to sell?")), newPrice = Double.parseDouble(JOptionPane.showInputDialog("Please input the new price of the " + myStocks[choice4 - 1].getName() + " stocks.")));
    								
    								if(testSell = true) {
    									myCustomers[choice1 - 1].successfulSell((choice1 - 1), newPrice, numberBought);
    								} else {
    									JOptionPane.showMessageDialog(null, "Invalid price entry.");
    								}
    								
    							}
    So does anyone know how I would go about this?

    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Checking if a Boolean Within Another Class Object is True

    The value returned by the buy(...) method is what you're after. Having said that, where do you call the buy method in the second bit of code?

    Note that method names (like variable names) should begin with a lower-case letter, not a capital letter, and so the method should be named buy(...) not Buy(...). Class names should begin with capital letters.

  3. #3
    Alpharius120 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Checking if a Boolean Within Another Class Object is True

    Quote Originally Posted by Fubarable View Post
    The value returned by the buy(...) method is what you're after. Having said that, where do you call the buy method in the second bit of code?

    Note that method names (like variable names) should begin with a lower-case letter, not a capital letter, and so the method should be named buy(...) not Buy(...). Class names should begin with capital letters.
    I was using the sell method as the example. The buy method is the same thing, just with the buy method being called rather than the sell.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Checking if a Boolean Within Another Class Object is True

    Quote Originally Posted by Alpharius120 View Post
    I was using the sell method as the example. The buy method is the same thing, just with the buy method being called rather than the sell.
    I would suggest that you not do this -- it is guaranteed to confuse us (at least me). Please understand that we are at a distinct disadvantage since we do not have your whole program in front of us, nor do we know any of the background information or your thought processes involved in this process. So if you are going to have us analyze a method, show us that method itself rather than a different one. How are we to assume that the methods are symmetric without seeing it?

    So having said that, your code posted has at least one minor problem and at least one major problem. The minor problem is that you're using a Boolean instead of boolean, with the latter being a primitive and the former being a reference type wrapper for the primitive type. Use the primitive type unless you have no choice (such as if you were putting them into a collection) as there's no need for the extra over-head that the wrapper type requires.

    The major problem is that you're re-assigning the variable it seems. You're doing this:

    Java Code:
    if(testSell = true) {
    which is trying to assign true to testSell. Does that even compile? It should never be done. I suspect you're trying to do this:

    Java Code:
    if(testSell == true) {
    Notice the difference? This tests if testSell is true or not, but even this shouldn't be done. Instead you should use:

    Java Code:
    if(testSell) {

  5. #5
    Alpharius120 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Checking if a Boolean Within Another Class Object is True

    Oh, okay, makes sense.

    Thank you very much, and thanks for the suggestions!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 2
    Last Post: 06-16-2012, 06:07 PM
  2. Replies: 3
    Last Post: 09-13-2011, 01:01 AM
  3. my simple boolean code keeps saying true
    By shazakala in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 11:30 AM
  4. Boolean always true?? What am I doing wrong?
    By Forty0ztoFreedom in forum New To Java
    Replies: 4
    Last Post: 03-02-2011, 07:13 PM
  5. Checking class of object
    By ianyappy in forum New To Java
    Replies: 2
    Last Post: 12-06-2010, 01:34 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
  •