Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Arraylist  RSS feed

 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am struggling a lot with an ArrayList file, and this new topic is about establishing that my arraylist has what I hope it does! I have read in a txt file, and I am trying to print the contents of the ArrayList to check the List is populated as I want it to be. I have attached the errors below the code snippet. ANy ideas??



File: C:\Documents and Settings\Lari\My Documents\Huddersfield\Programming\Coursework2\Vehicles.java [line: 166]
Error: cannot find symbol
symbol : variable resultCar
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code to print out the info in the araylist is...

SimpleIO.showMessageDialog(resultCar.toString());
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you are trying to use resultCar outside the while loop then it will error because it wont be able to find it.

Either print out inside the while or declare the Car object outside the while loop...

HOpe that helps,

John
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as variables declared inside a method are local to that method, variables declared inside a block -- i.e., inside a pair of curly braces -- are local to that block. Since your print statement is after the loop, and the variable is declared inside the loop, the print statement can't see the variable.

Now, the thing I tried to point out earlier, and that we need to address, is that this code



does, effectively, nothing. You basically ask carList for a bookmark (which is really all an iterator is) into itself, and then you ask for the car at the bookmark's current position, and do nothing with it. The next() method also advances to the next bookmark position, and so you travel along the list, requesting, and then ignoring, each item in the list. For this to do anything useful, you must actually dosomething with resultCar, inside the loop. And this is, I think, what you're trying to do:



Now, that's not a great way to display a list, putting up a dialog box for each list entry; you'll get tired of clicking very fast! Why not display the whole list at once? ArrayList.toString() returns a very nice display of exactly this. Try replacing the entire code snippet above with

SimpleIO.showMessageDialog(carList.toString());
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried that - thanks for the suggestion but it only displays in the dialkog box and one at a time. Also the data is not what I can see in the txt file, making me think something has gone wrong with the arraylist. I shall attach the filereader class and the txt file below. An example of a print out from the arraylist is...Car@fa706d

TXT FILE
<car><reg>AB04CDE</reg><make>Ford</make><model>Fiesta</model><colour>blue</colour><passenger_no>4</passenger_no></car>
<car><reg>FG05HIJ</reg><make>Honda</make><model>Accord</model><colour>silver</colour><passenger_no>4</passenger_no></car>
<car><reg>KL06MNP</reg><make>Landrover</make><model>Discovery</model><colour>green</colour><passenger_no>6</passenger_no></car>
<car><reg>PR06STU</reg><make>Volkswagen</make><model>Passat</model><colour>silver</colour><passenger_no>4</passenger_no></car>
<car><reg>W180YAB</reg><make>RollsRoyce</make><model>Corniche</model><colour>black</colour><passenger_no>4</passenger_no></car>
<car><reg>C204DEF</reg><make>Audi</make><model>A8</model><colour>red</colour><passenger_no>4</passenger_no></car>
<car><reg>J605PLE</reg><make>Citroen</make><model>Picasso</model><colour>yellow</colour><passenger_no>5</passenger_no></car>
<car><reg>AP06GTR</reg><make>Landrover</make><model>Discovery</model><colour>green</colour><passenger_no>6</passenger_no></car>
<service><service_no>13570</service_no><reg>J605PLE</reg><date>15:07:2006</date><miles>20000</miles><part_replaced>brake_pads</part_replaced><part_replaced>front_tyres</part_replaced></service>
<service><service_no>24680</service_no><reg>J605PLE</reg><date>21:07:2005</date><miles>12000</miles><part_replaced>windscreen_wipers</part_replaced></service>
<service><service_no>20089</service_no><reg>AB04CDE</reg><date>15:07:2006</date><miles>30000</miles><part_replaced>tyres</part_replaced><part_replaced>brake_pads</part_replaced></service>
<service><service_no>25643</service_no><reg>SS05VBE</reg><date>21:07:2006</date><miles>12000</miles><part_replaced>oil_filter</part_replaced><part_replaced>headlamp_bulb</part_replaced><part_replaced>air_filter</part_replaced></service>
<service><service_no>25997</service_no><reg>K99TRP</reg><date>01:08:2006</date><miles>36000</miles><part_replaced>sprocket</part_replaced><part_replaced>exhaust</part_replaced></service>
<service><service_no>12996</service_no><reg>W180YAB</reg><date>15:06:2005</date><miles>20467</miles><part_replaced>oil_filter</part_replaced><part_replaced>brake_pads</part_replaced></service>
<service><service_no>24892</service_no><reg>K99TRP</reg><date>01:08:2005</date><miles>26000</miles><part_replaced>indicator_bulb</part_replaced><part_replaced>clutch</part_replaced></service>
<service><service_no>30014</service_no><reg>GA04LCC</reg><date>05:08:2006</date><miles>10000</miles><part_replaced>oil</part_replaced></service>
<motorbike><reg>K99TRP</reg><make>Kawasaki</make><model>Ninja</model><colour>green</colour><load>25.5</load></motorbike>
<motorbike><reg>SS05VBE</reg><make>Honda</make><model>Hornet</model><colour>black</colour><load>15.25</load></motorbike>
<motorbike><reg>GA04LCC</reg><make>Yamaha</make><model>Raptor</model><colour>red</colour><load>20.00</load></motorbike>
<motorbike><reg>SS05VBE</reg><make>Triumph</make><model>Speedmaster</model><colour>black</colour><load>20.50</load></motorbike>

FILEREADER CLASS (VEHICLES)
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added a println to the end of each section of caode to read in bits of info, and this is giving the info from the txt, however the

Iterator<Car> carIterator = carList.iterator();
while (carIterator.hasNext()){
Car resultCar = carIterator.next();
console.println(resultCar.toString()); }

section still returns numbers such as... Car@f9f91d
Car@f9f91d
Car@129df8a

Intended read out is eg...

Car = AB04CDE
Car = Ford
Car = Fiesta
Car = blue
Car = 4

however these are followed by such output as Car@18d4c9a

eg
Car = AB04CDE
Car = Ford
Car = Fiesta
Car = blue
Car = 4
Car@18d4c9a
Car = FG05HIJ
Car = Honda
Car = Accord
Car = silver
Car = 4
Car@18d4c9a
Car@124e935
Car = KL06MNP
Car = Landrover
Car = Discovery
Car = green
Car = 6
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still no ideas as to what is happening with this... anyone out there have any ideas?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you are printing something that doesn't have the toString method overridden. this looks like the Object version of toString() that every class inherits.

I haven't gone through your code, so I can't tell you where. Look at every place where you print stuff, and make sure all are appropriate to be printed.

I'm not sure why you are printing it out like you are. it would make more sense to print out

Car Name = AB04CDE
Car Make = Ford
Car Model = fiesta
Car color = blue

I'm not sure what "car = 4" means (number of passengers? tires? radio station presets? years old?), but if you put in something else, it becomes self explanitory.

labelling them would also help you figure out where the weird printing is happening.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, indeed, Lari, the "Car@98739873" is what calling toString() on a Car instance -- or generally, just printing a Car instance -- will show, if you haven't defined the toString() method for the Car class. You want to write this method so that it returns whatever you'd like to see printed out for a Car, as a String.
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have looked for all my print commands, and have made the printout more clear (see below), I have noticed that after the first vehicle there is one @<number>, after the second, two of them, and so on... does this cast any light on why they are there?
Also I notice they all begin Car@, however there are 3 arraylists, car, bike and service. I'm clueless here I'm afraid

Car reg= PR06STU
Car make= Volkswagen
Car model= Passat
Car colour= silver
Car passenger number= 4
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car reg= W180YAB
Car make= RollsRoyce
Car model= Corniche
Car colour= black
Car passenger number= 4
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car reg= C204DEF
Car make= Audi
Car model= A8
Car colour= red
Car passenger number= 4
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car reg= J605PLE
Car make= Citroen
Car model= Picasso
Car colour= yellow
Car passenger number= 5
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car reg= AP06GTR
Car make= Landrover
Car model= Discovery
Car colour= green
Car passenger number= 6
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car@1f09a31
Service No=
reg= J605PLE
date= 15:07:2006
miles= 20000
part_replaced=
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car@1f09a31
Service No=
reg= J605PLE
date= 21:07:2005
miles= 12000
part_replaced=
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car@1f09a31
Service No=
reg= AB04CDE
date= 15:07:2006
miles= 30000
part_replaced=
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car@1f09a31
Service No=
reg= SS05VBE
date= 21:07:2006
miles= 12000
part_replaced=
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car@1f09a31
Service No=
reg= K99TRP
date= 01:08:2006
miles= 36000
part_replaced=
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car@1f09a31
Service No=
reg= W180YAB
date= 15:06:2005
miles= 20467
part_replaced=
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car@1f09a31
Service No=
reg= K99TRP
date= 01:08:2005
miles= 26000
part_replaced=
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car@1f09a31
Service No=
reg= GA04LCC
date= 05:08:2006
miles= 10000
part_replaced=
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car@1f09a31
Car@c8e4de
Car@12e4cd
Car@1ca5df9
Car@df9095
Car@18d4c9a
Car@1a02097
Car@3dfcb
Car@1f09a31
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you printing the Arraylist somewhere, or an iterator that you hit each time you add a new car? I'm kind of guessing here...

in other words, you add one car to the list. you then iterate over it, getting each element (a car). you somehow then print that.

you then add the second car to the list. you again hit an iterate-loop, printing each one again... and so on.

that would explain why you get 1, then 2, then 3...

if you really can't find where it's printing these by examing your code, liberally sprinkle in more print statements:

System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");

etc. you can figure out what two numbers the strange print is between. then put in more between those two, until you find the single line of code printing the strangeness.
You could also start by putting in something at the beginning and end of each method:

System.out.println("Entering method A");
// all the method code
System.out.println("leaving method A");

try that, and see if you can figure out where these are coming from...
[ January 26, 2007: Message edited by: Fred Rosenberger ]
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion, and I know what you mean, and with my limited Java knowledge I don't think an Iterator is used by the code until the whole txt file is read in, but what you say seems to make sense. I have a series of small sections to read in reg, make, model etc...
eg

if (line.indexOf("<car>")> -1){
// Select/Extract the registration element
int nStart = line.indexOf("<reg>");
int nEnd = line.indexOf("</reg>");
String reg = line.substring(nStart+5,nEnd);

for each element, then after I have gone through each car element...

Car car = new Car(reg, make, model, colour, passengerNoInt);
carList.add(car);

then the same basic code for bike and service elements, then...

I am not sure what to have ate this, as I have been playing with where to put the Iterators to get the desired output
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SUCCESS...I have overridden a toString() method and managed to establish that the txt file is being read in successfully, now back to my get method problem (will it ever end?!)

I am printing out the contents of arraylist to show what is there, then using the below get method to retrieve a make of car from a reg number, however the answer always comes bak as null... any ideas more than gladly received!

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lari Hopkins:
...I am printing out the contents of arraylist to show what is there, then using the below get method to retrieve a make of car from a reg number, however the answer always comes bak as null...

A guess: When you test equality of Strings, they must match exactly -- same case, same leading or trailing spaces, etc. So you must enter the reg exactly as it's stored.

Depending on whether your String values are meant to be case sensitive, you might use String's equalsIgnoreCase method in place of equals. Also see String's trim method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!