Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating a new object in an ArrayList

 
joanne robinson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chaps and Chapesses

I hope you dont mind me starting a new thread here. My last thread was getting cumbersome and I think I have moved my code on much in the last few days. In fact, my code is thankfully almost there (in a "working" sense anyway. A true Java programmer would be ashamed!)

Anyhow, I'm reading in code from a text file, adding it to an array and pulling it back out. Where I think I have an issue is with adding a new set of car details to the array. Currently it is overwriting the same array place with the next set of car details. So regardless of what registration I search on, be it bike or car, It always give the make, model, colour of the last piece of car data. (Which isnt last on the text file)

Hope that's not too long-winded but I have marked up in the code where I think my error is. Any help greatfully received.



many thanks
Jo
[ January 28, 2007: Message edited by: joanne robinson ]
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this excerpt of your code:

It should be

Same error on multiple locations in your code.
 
joanne robinson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I see, and that is because have create carList.add(c); ?? (Just so I understand it for next time).

However, it's still only returning the make, model or colour for the last car on the list regardless of if I ask for data about a car of bike!

I've tidied it up a bit but still have same issue:




The interactions pane is returning:

> java DemoFileReader
Landrover
Landrover
Landrover
Landrover
Landrover
Landrover
Landrover
Landrover
>

everytime!
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please post your Car and your Motorbike classes?
Perhaps the constructors Car(...) and Motorbike(...) aren't defined correctly.

Edit: Perhaps you accidently use static variables in these classes?

[ January 28, 2007: Message edited by: Anton Uwe ]

Edit2: You obviously use static variables ther, since you were able to compile your older version of your code containing i.e. "... Car.model"
[ January 28, 2007: Message edited by: Anton Uwe ]
 
joanne robinson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did use static because I was gettig the non-static variable compiler issue.

Here's car



and bike

 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just remove the "static" in all lines of those two classes...
 
joanne robinson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anton, you are a star* I have been struggling so long with this!

It is now printing (hurrah) however, it is printing all of the cars (colour for example) than the one related to the specific registration entered by the user.

any thoughts?
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's take a look at this sniplet:

The lines "System.out.println(..) will print out all the not-desired information. I would remove those lines in all your getXxxx()-methods.
Then take a look at this sniplet:

Here, the String make contains the desired information. But sadly, noone will print this info. I would add a "System.out.println(make)" after the line " String make = getMake(Search);" to see this info.
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just to clarify in this section of code,



The system.out.println's are printed outside of the if statement so it will print the make of every car except the one you want,



if you want it to print the car make and then return it you need to put this



Also dont forget that any code after the return will not be executed as the method will have returned, so you need to do all System.out's before the return.

John
[ January 29, 2007: Message edited by: John Bartlett ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic