• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with program. Can not find symbol.  RSS feed

 
Pat Rhodes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still new to arrays and arraylists. The fault lies in my constructor. Can anyone lend a hand as to what's wrong? I had to comment the faulty part out just so it would compile.

 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Pat, welcome to the Ranch!

I added the "code" tags to the code you posted, so it's more readable. (Don't you agree?) Could you do that in the future when you post code?

As for your question... well, what is your question? I don't really see a description of your problem anywhere. Have a look at our FAQ article TellTheDetails to see what I'm talking about.
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are re-declaring Car[] theCars on line 18. Is that intended, or did you mean to initialize the Car[] theCars array which was declared on 5?


It would be helpful if you post the actual error message.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont see the code you commented out. usually when you get a message like that from the compiler it means you didn't import something.
 
Pat Rhodes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to populate the array with 20 empty car objects and fill in the details when I make my tester. The error I'm getting is...

RentalCarCompany.java:21: cannot find symbol
symbol : constructor Car()
location: class Car
theCars[i] = new Car();
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't post the Car class, but that sounds like you don't have a no-arg constructor defined in the Car class.

 
Pat Rhodes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, here's the Car class.

 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create a constructor with arguments, you don't automatically get the compiler provided no-args constructor.

You have this one:

but you are trying to call new Car() (which doesn't exist)


 
Pat Rhodes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm supposed to add the arguments to new Car()?
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably, you created the constructor with the intent of using it, but you haven't done so.

You can change your call to include the arguments required for that constructor to something like

I hardcoded the values in the call. You can, of course pass them by variables.
 
Pat Rhodes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. I have to assign them values. I suppose I can give them all default values until I change them later with the tester. I appreciate this.
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're going to use default values, create a no-args constructor in the Car class and leave the call as-is.

All of your members except String will have their default values anyway (0 or 0.0). String members are null until you initialize them.
 
Pat Rhodes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if I were to change the Car constructor to a no args constructor, that would conflict with the addReservation method in the RentalCarCompany class. I think I'll just modify the RentalCarCompany constructor a little bit more.
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not change it... add it..

You can have multiple (overloaded) constructors, right?

Once you have them in place, you call whichever one is appropriate at the time.
 
Pat Rhodes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out, thanks a lot!
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i thank you too Keith, i didn't know that if i made a constructor with args that i wouldn't get the "free" no args constructor. well i did but i forgot.
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some of this stuff is still very fresh in my head from the SCJP prep. I find that if I can explain stuff to other people, I will remember it better myself.

I'm glad you found it useful!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree with the suggestion to create a no-arguments constructor. That would allow you to have Car objects with no name or (number of doors == 0) or similar. Don’t create code just so it will compile. Omit code so you can restrict the execution to what you want. You don’t want a Car without a name or 0 doors, so make sure you don’t have methods or constructors which allow that.
Why have you got a renter in the Car class? That looks peculiar. A Car does not have a renter, it is rented to several people at different times. You actually have three possible objects: a Renter, a Car, and a Rental. This actually looks like a database problem, but if you are going to do it in object-orientation, you can make Lists of Cars, Rentals and Renters.

If you don’t know how to make those lists, start with a List of cars and put cars in and get them out. Forget about renters until you have got that working. A lot of beginners make the mistake of writing lots of code; when they get into difficulties, they don’t know what went wrong. If you do it in little bits, trying out code after every few lines, it makes it much easier to work out what went wrong.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote: . . . the "free" no args constructor. . . .
What’s free about the default constructor? Look at this “how-to” page about javadoc, and you see they recommend always writing a constructor, so you don’t ever see default constructors. You don’t want to provide a way to make a class instantiable which you don’t know about. The same applies to regularly writing no-args constructors, which is why I argued against it in my previous post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!