Results 1 to 3 of 3
  1. #1
    killmenow is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Class and return statements question, not updating correctly

    Hey all, I'm trying to figure out how to write another class that deals with stock changes. This is what i have so far:

    public Stock(String name, double price) {
    this.price = price;
    this.name = name;
    }

    public String update(String sameName, double changeInPrice) {
    if (name == sameName && changeInPrice != 0)
    changeCount++;
    price += changeInPrice;
    if (name != sameName)
    return //this part is what i can't get
    return sameName;
    }


    so what i'm trying to get done here is when update is called like

    Stock s = new Stock("APL",21.5);

    s.update("APL",+.5);
    s.update("APL",0.);
    s.update("GOOG",-.5);
    s.update("APL",-1.0);

    i want to just return the price for whatever the stock name was called for. because later when i need to getPrice, it should return to me with 21.0. at the moment, it keeps returning 20.5, which means its also reading the GOOG when its not suppose to. when i get the changeCount is changed by any non-zero number, it comes out correct which is 2(so it doesn't count GOOG).

    is the correct way to "return nothing" when its not what i want? i tried to put a
    if (name != sameName)
    return...nothing?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Class and return statements question, not updating correctly

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Class and return statements question, not updating correctly

    You don't compare Strings using '==', since that only tells you if the two String objects are the same object, not whether the values of them are the same..
    You need to use equals().

    But, why are you doing this in the Stock class? It shouldn't have to do this check.
    That should be the responsibility of the class that controls all the Stocks, either as a check as above or via a Map<(name of stock), Stock>.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Using IF statement to control return statements.
    By Teclis in forum New To Java
    Replies: 21
    Last Post: 04-16-2011, 08:42 AM
  2. Replies: 7
    Last Post: 04-11-2011, 07:26 PM
  3. Return statements in methods
    By adjit in forum New To Java
    Replies: 12
    Last Post: 03-17-2011, 02:31 PM
  4. Code completion not updating correctly
    By kspn in forum NetBeans
    Replies: 1
    Last Post: 02-22-2011, 10:28 PM
  5. Static Method and Return Statements
    By berelson in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 11:17 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
  •