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
Ziga Klemencic
2,569 PointsNeed help whit code challenge
If the guess is correct, we want to alert the user with a message using an AlertDialog. Add an if statement that checks if the boolean variable isCorrect is true. Inside the if statement, declare a new AlertDialog.Builder variable named builder. Initialize it with its constructor.
i dont understand what im doing wrong, i checked if bollean isCorrect is true, and declared AlertDialogBuilder variable. pleashe help me :D here is the code :
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.widget.EditText;
public class GuessNumberActivity extends Activity {
protected String mUserInput;
protected EditText mNumberField;
/*
* onCreate() and other code has been omitted for brevity!
*/
public void makeGuess() {
// Set mUserInput here!
mUserInput = mNumberField.getText().toString();
// Check to see if the user's number is correct
if(isCorrect == true){
AlertDialog.Builder builder = new AlertDialog.Builder(GuessNumberActivity.this);
}
boolean isCorrect = GuessEngine.testGuess(mUserInput);
}
}
Thanks for your time :D.
3 Answers
Ben Rubin
Courses Plus Student 14,658 PointsThe line boolean isCorrect = GuessEngine.testGuess(mUserInput); needs to come before the if statement. The way you have it right now, isCorrect is used in the if statement before you assign it a value based on whether or not the user's guess is correct (and isCorrect is used before it is declared as a variable too)..
james white
78,399 PointsThe confusing part (trying to follow along with this thread answer) is that boolean IsCorrect was already provided as part of the stated code and only adding the if statement after that was necessary.
Here's a hint about using special forum markdown code: Line spacing does count!
So if you space the set of single quote marks too close to the code or the line before, your code gets greyed out.
Here's the "nice" (well marked up) version of the code:
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.widget.EditText;
public class GuessNumberActivity extends Activity {
protected String mUserInput;
protected EditText mNumberField;
/*
* onCreate() and other code has been omitted for brevity!
*/
public void makeGuess() {
// Set mUserInput here!
mUserInput = mNumberField.getText().toString();
// Check to see if the user's number is correct
boolean isCorrect = GuessEngine.testGuess(mUserInput);
if(isCorrect == true){
AlertDialog.Builder builder = new AlertDialog.Builder(GuessNumberActivity.this);
}
}
}
Ziga Klemencic
2,569 PointsThat totally worked :D thanks for your help :D