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

Android

Local variable 'colorAsInt' is redundant

Hi. In the Android Development - Build a Simple Android App by Ben there are no errors but is says that 'colorAsInt' is redundant.

Here is my code:

String color;
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mColors.length);
color = mColors[randomNumber];
int colorAsInt = Color.parseColor(color);
return colorAsInt;

How do I fix this? Thanks in advance, Peter!

2 Answers

Don't worry about that - it is a warning.

Essentially, the compiler is saying there's no point in declaring colorAsInt as you immediately return it without changing anything.

So, you have said:

int colorAsInt = Color.parseColor(color);
return colorAsInt;

the compiler is pointing out that you can write this and still achieve the same:

return Color.parseColor(color);

The colorAsInt is just there for clarity, rather than having any code necessity.

Make sense?

Steve.

Alright yes I understand it. Thank you so much, Steve :)

No problem! :-)