Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 830847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:06:39+00:00 2026-05-15T04:06:39+00:00

A while back, I was working on a program that hashed values into a

  • 0

A while back, I was working on a program that hashed values into a hashtable (I don’t remember the specifics, and the specifics themselves are irrelevant to the question at hand). Anyway, I had the following code as part of a “recordInput” method:

tempElement = new hashElement(someInt);

    while(in.hasNext() == true)
    {
        int firstVal = in.nextInt();
        if (firstVal == -911)
        {
            break;
        }
        tempElement.setKeyValue(firstVal, 0);
        for(int i = 1; i<numKeyValues;i++)
        {
            tempElement.setKeyValue(in.nextInt(), i);
        }

        elementArray[placeValue] = tempElement;
        placeValue++;

    }   // close while loop

} // close method

This part of the code was giving me a very nasty bug — no matter how I finagled it, no matter what input I gave the program, it would always produce an array full of only a single value — the last one.

The problem, as I later determined it, was that because I had not created the tempElement variable within the loop, and because values were not being assigned to elementArray[] until after the loop had ended — every term was defined rather as “tempElement” — when the loop terminated, every slot in the array was filled with the last value tempElement had taken.

I was able to fix this bug by moving the declaration of tempElement within the while loop. My question to you, Stackoverflow, is whether there is another (read: better) way to avoid this bug while keeping the variable declaration of tempElement outside the while loop.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:06:40+00:00Added an answer on May 15, 2026 at 4:06 am

    Why would you want to keep the variable declaration outside the while loop? Anyway, you can, as long as you assign it to a new hashElement each time:

    hashElement tempElement;
    while (/*...*/) {
        tempElement = new hashElement();
        //...
    

    It’s certainly not “better” though. Scope your variables as narrowly as possible, in general.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm back with another similar question. I am currently working on a Java program
I'm working on an online forex trading system and a while back we have
A while back, I remember using a piece of code (4-5 lines), added to
A while back a found a great-looking framework that allowed .net developers to implement
A while back I wrote a simple python program to brute-force the single solution
A while back I created a lightbox plugin using jQuery that would load a
a while back, CNET had these really cool Social Share buttons, that when you
A while back I was playing with methods using variable-length argument lists (java) that
I remember a while back reading about editing the proxy pac file which would
I've been working on a GUI program for a while now, and I just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.