Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arraylist

 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolute beginner here. I have an araylist to construct and a txt file to be read into it, any tips on how to do this will be gratefully received.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us what you have so far, and tell us where you're stuck.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following tutorials might contain useful information:

Collections (about how to use collection classes like ArrayList)
Basic I/O
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my filereader so far, I have got a readout from the txt file, but am having problems adding them to arrayLists. I have added the following but then taken it out as it doesn't seem to work, which I think is a problem with my constructor in Car.java. Any tips on setting up the constructor?



EDIT by mw: Added Code Tags.
[ January 19, 2007: Message edited by: marc weber ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using code tags when posting code in future - it makes it easier to read.
Having said that I can see a couple of problems.

You declare carlist as an ArrayList that contains Strings, but you are adding Car objects to it. Change the declaration to


Later on in your code you try to assign a Car object to carlist

Get rid of the middle line, not only is it wrong, you don't need actually need it as you have already created your ArrayList.

Make those changes and if it still doesn't work, tell us what the problem is - if it doesn't compile tell us what the error messages are - if it doesn't do what you want, tell us what it is doing and what you expected it to do.
 
Lari Hopkins
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I have made those changes, however I still have a Car class with no constructor as I'm not sure what to write. Also the following compilor errors have come up:

3 errors found:
File: C:\Documents and Settings\Lari\My Documents\Huddersfield\Programming\Coursework2\DemoFileReader.java [line: 61]
Error: cannot find symbol
symbol : constructor Car(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Car
File: C:\Documents and Settings\Lari\My Documents\Huddersfield\Programming\Coursework2\DemoFileReader.java [line: 61]
Error: internal error; cannot instantiate Car.<init> at Car to ()
File: C:\Documents and Settings\Lari\My Documents\Huddersfield\Programming\Coursework2\DemoFileReader.java [line: 62]
Error: non-static variable carList cannot be referenced from a static context


Lines 61/2 =
Car car = new Car(reg, make, model, colour, passenger_no);
carList.add(car);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic