• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing variables - help with code  RSS feed

 
Natasha Reaney
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone.

I am completely stuck on this assignment. Can anyone help? With the online class I am taking, there is a discussion forum, so I got some help on there but I am unsure as to why the advice given works and how I know when to use the code they provided. My actual questions are within the code below. I would appreciate any assistance/clarification. Thank you!!

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37256
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tempSearchResults.name is writing to tempSearchResults before you write new XXX. Which is why it is complaining about initializing.

What is the code supposed to do though? It seems like you could just add oneRows to the result without a temp variable of the same type.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you get the initialization error is because you do not create a new GeoRow when you make the temp variable. You try to use it by setting its values - but there is nothing there to use.

For the reason you need the temporary value: the code you posted doesn't indicate why you need one. I am guessing there is more to this problem then you have told us so far. What was the original problem that the other forum told you to use a temp variable to solve? Does the GeoRows iterator re-use the instance of GeoRow it returns (i.e. when you put oneRow into the list you get a list filled with the last row over and over again?


edit-- I'm too slow
 
Natasha Reaney
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable is considered a reference variable, correct? So when I initialize it with the code below, I am creating a new object/instantiating a class? When I am using reference variables do I always initialize them to new?

It's a fairly long problem. We are reading in data from a file, copying the data to a list, promting the user for a search term(name), searching the file to see if the search term (name given) matches a name in the name column of one of the rows in the list. If so, we need to add the row to a new list, searchResults and then print the list to a file.



 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Natasha Reaney wrote:The variable is considered a reference variable, correct? So when I initialize it with the code below, I am creating a new object/instantiating a class? When I am using reference variables do I always initialize them to new?

You either need to create a new one (like you do in the example you posted) or you need to assign the variable a reference to one that has already been made.


None of that really explains why you need the temporary variable though, or why you feel you need to fill it with the data from oneRow before you put oneRow into the results list.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!