aspose file tools*
The moose likes Beginning Java and the fly likes constructor to add to an array list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor to add to an array list" Watch "constructor to add to an array list" New topic
Author

constructor to add to an array list

joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
Hello all

I think one of my course mates has asked a couple of questions in the last week so the code objective may seem a bit familiar! I am trying to read in data from a text file (which I have done) store it as a variable and then add it to an array. I seem to be very out of my depth and I am struggling on the what goes into what file and exactly how to construct th constructor! (Are you following because I think I have lost myself!!)

Anyhoo,

I have a file reader where I declare the arraylist (I'm, only concentrating on car data for now) and a car.java file to store the data. Both are copied below and the compiler is geting upset with the lack of identifier in the last line of car.java. However, I think my problm is a bit bigger than that because as mentioned I think it is the placement of the code thats at fault. Any suggestions would be warmly welcomed as I about to keel over with stress!

The Filereader



and the car.java (don't laugh too much!)

Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
I'm new to Java, but it looks like your constructor isn't set up correctly.

In Car.java you may try...


Also, you can't convert directly from a String to an Integer. If you do want to pass an integer to passenger_no you'll need to convert it from a String in your Demo file.



Hope that helps.
[ January 24, 2007: Message edited by: Nathan Leniz ]

The very existence of flamethrowers proves that at some time, some where, some place, someone once said to themselves "I'd really like to set those people on fire over there, but I just can't get close enough".
joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
thanks for the response. I am no getting the following errors in demofilereader:



and in car.java


urm, I think i'm thick
joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
can anyone help?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

//Error: class Car is public, should be declared in a file named Car.java

This one is easy. it tells you the problem. you have a Car class, that you say is defined in a car.java file. that is not going to work. change the name to Car.java (even if your operating system is not case sensitive, Java still is).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

Car c = new Car (reg, make, model, colour, passenger_no);
What are the variables you are passing in? the first four are Strings. the last, passenger_no is an int.

Look at Nathan's post on how to set up a constructor for the Car class.
joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
lol - sorry I had changed that but obviously hadn't closed and reopened it. That's fine now but still unclear about the last three errors

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

(in case you haven't noticed, we are tackling the errors one at a time. The first error can hide other errors, or cause other false errors to show up. Anytime you get compilation errors, always try and fix the first one first, then recompile before tackling any others - at least until you are more confident what they all mean)

if your car class still looks like what you have posted in your first post, it won't work.

you are making the call:

Car c = new Car (reg, make, model, colour, passenger_no);

note that reg, make, model, and colour are all Strings. passenger_no is defined as an int.

The compiler is telling you it can't find a constructor for the Car class that takes a (String,String,String,String,int). it can find a (String,String,String,String,String) constructor.

do you see the difference?

Nathan has shown you how your Car class should be written. I am somewhat surprised your Car class compiles at all. The compiler may be confused if you had a car.java that did comile at some point... I'd make sure you delete any car/Car.class files, make sure your Car.java file is named right, then re-compile.
[ January 24, 2007: Message edited by: Fred Rosenberger ]
joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
thanks for your help, it's much appreciated. I understand your point about the int, and as I cannot workout how to change the string to and int I have remove passenger number. A cop-out I know but I just want to get the bones of it working for now.

The good news is I am just down to one error. Thanks to both Nathan and Fred for getting me this far )

The newest error is:

joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
sorry, that should heve been a
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

Originally posted by joanne robinson:
sorry, that should heve been a

FYI - You can edit any of your own posts by clicking the paper/pencil icon at the top of the post you want to edit.

as to the current error... this is another common problem. in your definition of DemoFileReader, you say this:

"Evertime i make an object of the type DemoFileReader, I want it to have the following variables: carList, bikeList, and serviceList. Now, regardless of how many (if any) DemoFileReaders I create, there needs to be a SimpleFIO and a SimpleIO - these exists no matter what, and I can always refer to them".

the difference is that "static" keyword before you declare them.

you then go into your main method. no DemoFileReader is ever created, so there are no XXXLists created.

You could either make all those lists static, and thus they'd exist whether you create a a DemoFileReader or not. or you could create a DemoFileReader object, then refer to the xxxList inside THAT object.
joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
Fred, you are a star and have prevented a nervous breakdown today. Thank you so very much!

I am by no means done, as I have to pull the blooming data back out again now. But I will try my hardest to push on without bothering you again.

thanks again, and also thanks to you, Nathan
Jo
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

you're not bothering me, or anyone else with the questions. That's why we're all here. Please ask more questions. If I can't or don't have time, I won't answer, but someone else will.
Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
Glad I could help Joanne. I still ask a lot of questions myself and the people here at the ranch have been invaluable and more than kind in helping me. What the ranch is all about.

Keep asking more questions, it's how we ALL learn. I know I've learned a lot reading other people's questions, so "bother" away!


Nate
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor to add to an array list