Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Harnessing the Power of Objects Helper Methods and Conditionals

creating an eatPez(); method

I am trying to create a method that "eats" a PEZ candy and then reduces the mPezCount by one and then returns a silly string about eating the candy and the current count. I get keep getting an error about not being able to convert int to string, but I don't see how that's happening or why. Any Advice?

Hey guys, thanks for the responses. I figured it out. It was the fact that I had it as a String instead of an int. And I believe that I had the method doing too many things. Once I slowed down and separated things out it figured it out.

1 Answer

Hello

I suspect you are returning mPezCount by mistake

Let me explain:

if you have a method like this

public String eatPez(){ if(mPezCount > 0) { mPezCount =- 1; return mPezCount ; //this is wrong }else { return "No Pez to eat. Please add Pez"; } }

what you may be looking for is

public String eatPez(){ if(mPezCount > 0) { mPezCount =- 1; return "YUM! YUM!" }else { return "No Pez to eat. Please add Pez"; } }

Hope this helps

If this answers your question, please mark as answered.

Thanks