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 trialInge Haagensen
187 PointsThe last challenge. Cannot solve it! Do you maybe have the code I can get
Looping until value...
String who = orange;
Boolean isInvalidWord;
do {
console.printf("Knock Knock.\n");
String who = console.readLine("Who's there? ");
console.printf("%s who?\n", who);
isInvalidWord = (who.equalsIgnoreCase("banana"));
if (isInvalidWord); {
console.printf("Knock Knock.\n");
String who = console.readLine("Who's there? ");
console.printf("%s who?\n", who);
}
} while(isInvalidWord);
String who = console.readLine("Orange you glad I didn´t say Banana again? ");
Inge Haagensen
187 PointsHi Gloria. I am so happy for you quick answer although I do not understand it. I am new to Java and Treehouse. Where can I view the code, you have fixed? :-)
The video is it: Looping until the value passes? (I have view it many times, put has not quit gotten the answer)
Gloria Dwomoh
13,116 PointsThe video is about how to post code in the forums. You can see it by viewing the video on the right next to "Tips for asking questions". Before fixing your code it looked like plain text.
3 Answers
Gloria Dwomoh
13,116 PointsThe question is asking something more simple than that.
It wants to you run this...
console.printf("Knock Knock.\n");
String who = console.readLine("Who's there? ");
console.printf("%s who?\n", who);
till the person gives input Orange...once the person does you print "Orange you glad I didn't say Banana again?"
Edit (more details):
This line of code is wrong
String who = console.readLine("Orange you glad I didn´t say Banana again? ");
what it does is read...what the user inputs after printing the line "Orange you...." and assings it to the variable who. The question though wants you to just shift the definition of "who" outside the do - while block.
So it wants you to put
String who;
outside the do while block (before it...since you need it in the do-while block)
In general what it is asking for is what I have written below. Take notice of the comments - explanations I have added
//Here is the prompting code
String who ;
do{
console.printf("Knock Knock.\n");
who = console.readLine("Who's there? ");
console.printf("%s who?\n", who);
} while (who.equalsIgnoreCase("banana"));
/* do this loop above as long as the user inputs Banana...once it stops
giving banana and the user gives something else...such as orange or anything else,
break the loop.*/
console.printf("%s you glad you didn't say Banana again?", who);
/*then print the fruit or thing the person mentioned in the form
"[User input] you glad I didn´t say Banana again? "
*/
I hope this makes things more clear :)
Jason Anello
Courses Plus Student 94,610 PointsHi Gloria,
Since we don't receive notifications for edits it's a good idea to leave a quick short comment saying you've done so. That way the questioner and any subscribers will know to check back. :)
Gloria Dwomoh
13,116 PointsHey Jason, I had the first half as an answer initially then I deleted my previous answer (added that and the new edited part) and made a new answer adding both together so Inge can get a notification. I got that covered. ^^
David H
Courses Plus Student 5,467 PointsLast challenge was interesting for a couple of reasons.
As a new programmer, the declaration of the empty 'who' String and referencing the 'who' String object inside the 'do' block was a bit confusing, but I got it now.
Secondly, it's hard to know what objects the challenge section has already created. I know that the 'console' object was created in a couple of challenges.
Again, this is all new to me, so I'll keep plugging away! :)
Steve Kinsey
4,463 PointsYeah, I found the last challenge a bit difficult, but I think mostly because I didn't 'get' the joke rather than not understanding the code to be honest. I got the code sorted in the end, but I still don't get the joke.
Practiced the do / while loop with a simple program asking the user to try to guess which primary colour I was thinking of (program loops until they enter green...I used Scanner as I had issues using Console in JCreator)
import java.util.Scanner;
public class primaryColour {
public static void main(String[] args) {
String primaryColour;
Scanner user_input = new Scanner(System.in);
do {
System.out.print ("Red, blue or green? ");
primaryColour = user_input.next();
} while (primaryColour.equalsIgnoreCase ("red") || primaryColour.equalsIgnoreCase ("blue"));
System.out.print ("Yup, green it is!");
}
}
Gloria Dwomoh
13,116 PointsGloria Dwomoh
13,116 PointsHi Inge, I fixed your code a bit. To learn how I did it you can check the video on the right.