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!!)
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!
and the car.java (don't laugh too much!)
Joined: Nov 26, 2006
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".
Joined: Jan 24, 2007
thanks for the response. I am no getting the following errors in demofilereader:
//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).
Never ascribe to malice that which can be adequately explained by stupidity.
(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 ]
Joined: Jan 24, 2007
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 )
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.
Joined: Jan 24, 2007
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.