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

    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; = name;

    public String update(String sameName, double changeInPrice) {
    if (name == sameName && changeInPrice != 0)
    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);


    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)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    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
    Rep Power

    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, 03:31 PM
  4. Code completion not updating correctly
    By kspn in forum NetBeans
    Replies: 1
    Last Post: 02-22-2011, 11:28 PM
  5. Static Method and Return Statements
    By berelson in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 12:17 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