#Looping Error

10 messages · Page 1 of 1 (latest)

obtuse wharf
#

Hi, so recently I’ve been having this specific issue in a project where I have a class that contains a name parameter which I use to grab a file from my laptop. But for some odd reason, every time I finish loading in a set of strings into an array of these classes, the entire array becomes the last element.

I’ve printed every single step of the array as it goes and it works perfectly but as soon as it finishes the entire array suddenly becomes a duplicate of the last element. To combat this I attempted just finishing up the last few lines of my code inside the loop but there’s no way for me to do that efficiently since I would end up nesting different loops and it would increase runtime. I was wondering if this is a general error that happens often under certain circumstances and what I could do to fix this

hexed fractalBOT
#

This post has been reserved for your question.

Hey @obtuse wharf! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

compact hill
#

sounds like you've constructed your loop incorrectly, show your code

obtuse wharf
#

@compact hill
This is my loop, pr is a scanner running through the file in the second image and inputting each line as the name parameter in the “Votes” Class

The votes class is the 3rd and 4th images, my grab method is static and takes in two parameters for name and year and I use those to grab a file. (File name example: GA2016.txt)

compact hill
#

which structure is haven't the issue exactly?

obtuse wharf
# compact hill which structure is haven't the issue exactly?

This Map called “cv” which uses a String and a CountyVote, once the loop ends, the entire Map becomes a duplicate of the last entry in cv. so for example I’ll have a bunch of different Strings that are either “red” or “blue” but if the last string is red when the loop ends then every single string becomes “red”

compact hill
#

v is a static field that keeps getting updated rather than a local map that gets reset in Votes.grab

obtuse wharf
#

Ohhhh ok Thank you

hexed fractalBOT
# obtuse wharf Ohhhh ok Thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.