aspose file tools*
The moose likes Beginning Java and the fly likes creating a new object in an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating a new object in an ArrayList" Watch "creating a new object in an ArrayList" New topic
Author

creating a new object in an ArrayList

joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
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

Joined: Jan 10, 2007
Posts: 122
Take a look at this excerpt of your code:

It should be

Same error on multiple locations in your code.
joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
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

Joined: Jan 10, 2007
Posts: 122
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

Joined: Jan 24, 2007
Posts: 15
I did use static because I was gettig the non-static variable compiler issue.

Here's car



and bike

Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
Just remove the "static" in all lines of those two classes...
joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
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

Joined: Jan 10, 2007
Posts: 122
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

Joined: Jan 25, 2006
Posts: 116
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating a new object in an ArrayList
 
Similar Threads
.get method data from an array using simple IO
constructor to add to an array list
Arraylist
HELP!
Arraylist