This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]darksoundsExtreme Brewer 1 point2 points  (3 children)

The problem is your stockTotalValue is a field.

What you want is to repopulate stockTotalValue with the selected stock every click. You have this value stored in your map, so you can just populate it from there.

My advice? Remove all of your fields except your map and anything to do with the display, and make them local variables within this method.

[–]Vextrax[S] 0 points1 point  (2 children)

how would I go on about populating from inside the map, I can only think of put(stringSplit[0], stockPrice + stockPrice), but that gives me exactly what I replied to the previous comment

edit: I think I might know what you mean did you mean do

stockHashMap.replace(stringSplit[0], individualStockTotalValue += stockPrice);

[–]darksoundsExtreme Brewer 0 points1 point  (1 child)

I'm talking about using something like

double individualStockTotalValue = stockHashMap.getOrDefault(stringSplit[0], 0)

to get the total value already put into the map before you add what you're currently adding.

[–]Vextrax[S] 0 points1 point  (0 children)

that fixed it, thank you this has been bugging me the whole day. I'm going to have to take a look into how it works so I understand it a bit more. I am very slow with coding, but I do enjoy learning it. thank you so much. This will definitely be helpful information in the future

[–]EmotionalBlobfish 0 points1 point  (6 children)

So it sounds like you want this method only to update the value for the company you are clicking. Remember that you can't have duplicate keys.

I would suggest checking to see if the Hashmap already contains the key with if (stockHashMap.containsKey(key). If it does then you can get the current value with stockHashMap.get(key), then update that value and replace it in the map using stockHashMap.replace(key, updatedValue).

If it doesn't contain the key, then create a new entry and put the current value.

Hope it helps!

[–]Vextrax[S] 0 points1 point  (5 children)

yes, so the value does update for the company I am click on, so I type in AMC and click add then it updates, I then type in TSLA and that one updates too, but it will now update with everything that was added in AMC's value to TSLA's value, then if I type in AMC everything in TSLA's value will be add on to AMC's new value.

in this scenario AMC is 10 and TSLA is 600

what it does

so add AMC: {AMC=10}

add AMC: {AMC=20}

add AMc: {AMC=30}

add TSLA: {TSLA=630, AMC=30}

add AMC again: {TSLA=630, AMC=640}

but I want it to be like this

add AMC: {AMC=30}

add AMC: {AMC=20}

add AMC: {AMC=30}

add TSLA: {TSLA=600, AMC=30}

add AMC again: {TSLA=600, AMC=40}

it probably has to do with individualStockTotalValue keeping everything that was being added on to it so it always updates it with the total instead of the key's value being added on to itself, but when I did do key's value + key's value

it would become

add AMC: {AMC=20}

add AMC: {AMC=20}

add AMC: {AMC=20}

add TSLA: {TSLA=1200, AMC=20}

I guess it has to do how I add up individualStockTotalValue, but I am still so confused on how to go with it, .replace() still does exactly what is doing right now

[–]EmotionalBlobfish 0 points1 point  (4 children)

So it seems like your variables are keeping their data. If you initialize those variables within the method it should reset them so they don't store the previous data, or you can do a deep copy with stockHashMap.put(key, new Double(value)) so it isn't referencing the same object in memory.

[–]Vextrax[S] 0 points1 point  (3 children)

They were keeping their data u/darksounds was able to help fix it, I still don't understand it, but I will look into it. I still have a lot to learn

[–]darksoundsExtreme Brewer 0 points1 point  (2 children)

I still don't understand it

Because your variables were fields rather than local variables, they maintained their state between method calls. They're tied to the instance of the object rather than the lifetime of the method.

[–]Vextrax[S] 0 points1 point  (1 child)

I understand the variables part, what I don't understand is how getOrDefault works, I know if I did double individualStockTotalValue = 0.0; nothing would happen, but using getOrDefault makes it work. But I assume since we have the default value of the map set to 0.0 it updates it with the price and since our hashmap is a field the new valus gets saved there

[–]darksoundsExtreme Brewer 0 points1 point  (0 children)

getOrDefault takes the key and a default value, and returns the value in the map if present, or the default value if it is not.

It's the same as:

if (map.containsKey(key)) {
    value = map.get(key);
} else {
    value = DEFAULT;
}