Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array losing info in loop

 
Daniel Hyslop
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to fill an array with String objects using a for loop.Thestring objects are assigned a hash code(has been change to int value).The hash has to to be matched with an element within an array When I pass this hash through a set of if statements to check it`s validity and save it to one of the elements within the array when the loop completes its first iteration it loses the info saved and I end up just saving one String each Iteration.I wonder if someone could shed some light on how I overcome this
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the code so we have a chance to point out the problem. Put your code between [ CODE ] ... [ /CODE ] tags (remove the spaces inside the brackets) to preserve formatting.
 
Daniel Hyslop
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not the finnished article as the validity test only checks for matches within the for loop at the moment .It works from another class where the has is worked out but I have added a main method at the bottom to show what should be happening.Here`s the code
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the loop inside main(), you create a new Storage class for each iteration. Since each Storage has its own hashArray, each will store a single name_course in a single slot.

Instead, create a single Storage instance before the loop and store each name/course pair in the same Storage using a different slot. Given that the slot choice is random, I'm not sure how you'll go about doing it.

Also, since Storage is going to store name/course pairs, it probably shouldn't have name and course instance variables itself. I recommend creating a different class to represent a student's information (name and course).

Is the goal for this assignment to create your own hash table? If so, note that in a hash table, many objects may hash to the same slot.
 
Daniel Hyslop
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks david,
The main main method in this class is not the one that I m using to store the values.The values are called from another class . Name and Course have been concanated and the hash is not random.It is generated in another class from a hashing algorithm using the name object.Just couldn`t figure out why I was getting individual entries.
thanks again for the help
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, but does the other code you're using create a single or multiple Storage objects? If multiple, this is the problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic