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
Holly Fox
2,808 PointsSwitch statement doesn't recognize the variable I want to test
I have a button which has an int value assigned to it when clicked. I then want to run code based on the value within a switch statement.
I only have 1 case at the moment but it doesn't seem to be picking up the variable I want to test.
The data is nested in the OnClickListener and the switch statement is in the OnCreate method. Has this got something to do with it? What am I doing wrong?
2 Answers
Rebecca Rich
Courses Plus Student 8,592 PointsAre you using Java JDK 6 (the default for Android Studio) or did you install Java JDK 7?
For now, let's try switching over to an if-statement (for now you could just try the first part with the check for "0"):
if (numberAsString.equals("0")) {
top.setBackgroundColor(Color.BLUE);
} else if (numberAsString.equals("1")) {
// Insert your code here for "1" case
} else {
// Insert your code here for all other cases
}
The else statement will catch numberAsString = "2" and any other value of numberAsString aside from "0" or "1" ... You could also make that line an else if statement, either is fine with the current looping you have for the number.
Does this work?
Holly Fox
2,808 PointsHi That works perfectly, thank you!
I'm not sure what Java JDK I'm on but I haven't installed anything so I presume its the default. I'm using BETA 0.8.2
Rebecca Rich
Courses Plus Student 8,592 PointsThe onCreate method will be called when the activity is created so if you want to change the button's background color dynamically (with clicks), you should move that logic (the switch statement) into your click listener so that each time you click the button that code will be evaluated and the background color of the button will change.
Holly Fox
2,808 PointsHi Rebecca! Ok great, that makes sense. I have moved the code into the onClickListener and my first problem is resolved...
I have a new problem! The switch says that my numberAsString is incompatible. But the case to test has zero in double quotes which is equivalent to a string isn't it? So not sure why its not working ie. "0"
Heres the revised code:
View.OnClickListener topListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
number++;
if (number == 3)
{number = 0;}
String numberAsString = number+"";
top.setText(numberAsString);
switch (numberAsString) {
case ("0"):
top.setBackgroundColor(Color.BLUE);
break;
}
}
};
Thanks for your help.
Holly Fox
2,808 PointsHolly Fox
2,808 PointsHere is my code: