• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error convert object to int  RSS feed

 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an error in my code ("Error convert object to int"), I have two class, a, class Personn with his attributes, and a class Hotel which has 2 functions checkout and checkin, the problem is:  I can't make person++ or -- because it's an objet , so i don't know how to make person++ in the function without eror, thanks






 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like instead of
you'd want
As you found out, you can't increment or decrement an object.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. The ++ operator can only be applied to primitives representing numbers (or unboxed objects). You may want the count of people rather than a Personne object.
 
Emma Sophia Jones
Greenhorn
Posts: 19
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pierre,

It looks to me as though you need to use some kind of container to store the different people in. Something like an ArrayList or HashMap depending on your needs.

That way, you can add a new person to the container when they check in, and take them out of the container when they check out.

You can't just create a new person by using ++ for a number of reasons, but one is that you need to specify the name, stayDuration and numberOfStays for every new personne that you create. ++ only really works for integers (as far as I'm aware) and is a shortcut for adding one to whatever is there already.

Are you familiar with ArrayLists and HashMaps or could you use a little more guidance?
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, y i think you're right emma, i'll try to make an arrayList()

(Carey, no it has to be protected for this exercice)
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I am not familiar with arraylist, i have an error, "convert arraylist to int" someone knows?

 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I putted String instead of Integer in ArrayList but i changed and i still have the error, cannot convert ArrayList to int :/*******

 
Emma Sophia Jones
Greenhorn
Posts: 19
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the goal of the assignment Pierre?

Are you just trying to keep track of the number of guests in the hotel? Or do you need to be able to retrieve information about them while they're staying there?
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emma Sophia Jones wrote:What's the goal of the assignment Pierre?

Are you just trying to keep track of the number of guests in the hotel? Or do you need to be able to retrieve information about them while they're staying there?


Yes i ll need to have the information about them after
 
Emma Sophia Jones
Greenhorn
Posts: 19
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And would you like to keep track of them by name or by, say, room number?
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emma Sophia Jones wrote:And would you like to keep track of them by name or by, say, room number?


Yes, by name;)
 
Emma Sophia Jones
Greenhorn
Posts: 19
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so what you’re probably wanting is a HashMap – this is a container which stores a value (which can be any object) against a key (which again can be any object but is pretty often a String). When you call the method .get(<key>) on your HashMap it will return the value associated with that key.

So let’s say we have a HashMap where a person’s age is stored against their name, I can pull out Sally's age by saying
If you’re still a bit uncertain on HashMaps, I’d have a bit of a google and see if you can find a tutorial before you continue reading my post
When we’re creating the HashMap, we need to specify the type of key it will have and the type of value it will store – so something like this:

Once we have that, we can start putting in new guests with a .put() command.

You can then get people out of the HashMap with get():

And also remove them on checkout with remove():

To get this to work you’ll need to rewrite quite a lot of your hotel class as your approach so far probably isn't going to work. It might be worth starting from scratch instead of trying to slot it into what is there already. Does that help?
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emma Sophia Jones wrote:Okay so what you’re probably wanting is a HashMap – this is a container which stores a value (which can be any object) against a key (which again can be any object but is pretty often a String). When you call the method .get(<key>) on your HashMap it will return the value associated with that key.

So let’s say we have a HashMap where a person’s age is stored against their name, I can pull out Sally's age by saying
If you’re still a bit uncertain on HashMaps, I’d have a bit of a google and see if you can find a tutorial before you continue reading my post
When we’re creating the HashMap, we need to specify the type of key it will have and the type of value it will store – so something like this:

Once we have that, we can start putting in new guests with a .put() command.

You can then get people out of the HashMap with get():

And also remove them on checkout with remove():

To get this to work you’ll need to rewrite quite a lot of your hotel class as your approach so far probably isn't going to work. It might be worth starting from scratch instead of trying to slot it into what is there already. Does that help?


Thanks, i think it's more clear for me now;)
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emma Sophia Jones wrote:(...)


I'm not happy with this, to be frank. Where is this "new Personne(name, stayDuration, numberOfStays)" coming from? I.E. how are the variables stayDuration and especially numberOfStays determined?

To me, it makes more sense if the Hotel had some guest Book, containing a collection of Personnes, with information about the number of Stays. When a new (or old) Personne arrives, then the guestbook is updated,
and only then would you register the guest in a Map<Integer, Personne>, where the integer is perhaps the room number. You do not need to monitor the number of occupied rooms in that way.

To easily find a Personne by name (or address or whatever), you could have a dedicated Map<PersonneProperty, Personne>, but I would only consider this if the List of Personnes becomes quite big.

And this is assuming that the Hotel only has one-Personne rooms.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pierre Gadar: Just a note on posting etiquette.  Since this is a forum and not a mailing list, it is preferred that you do not quote the entire post before yours.  It's okay if it's short, otherwise it  clutters up the topic.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!