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
Derek Glenn
3,548 PointsI can't find my problem?
import java.io.Console;
public class TreeStory {
public static void main(String[] args) {
Console console = System.console();
/* Some terms:
noun - Person, place or thing
verb - An action
adjective - A description used to modify or describe a noun
Enter your amazing code here!
*/
String ageAsString = console.readLine("how old are you?");
String adjective;
int age = Integer.parseInt(ageAsString);
if (age < 13){
console.printf("sorry you must be 13\n");
System.exit(0);
}
String name = console.readLine("Enter your name: ");
adjective = console.readLine("Enter an adjective: ");
do{
if (adjective.equalsIgnoreCase("gay") || adjective.equalsIgnoreCase("bitch"))
console.printf("do it again fucker");{
}
}while(adjective.equalsIgnoreCase("gay") || adjective.equalsIgnoreCase("bitch"));
console.printf("%s is very %s", name, adjective);
}
}
Derek Glenn
3,548 PointsIt just loops do it again. and i cant get passed it
Derek Glenn
3,548 Pointsi would love to ask you some questions? can you email me? derek101225@gmail.com
Boban Talevski
24,793 PointsFeel free to post multiple questions here so that multiple people can see them and provide answers, it's better for the community :).
2 Answers
Boban Talevski
24,793 PointsThis is my forked workspace from the time I was taking this course. https://teamtreehouse.com/workspaces/32990312
Notice how it has a boolean variable named isInvalidWord which is used to determine whether the while loop should go on. And it is set inside the while loop depending on the word entered.
And you do need to give the option to the user to change the word while inside the while loop, otherwise it will go on forever as Yanuar said. There's a slight problem with his solution, which is that it assumes outright that the user entered a bad word initially and is outright asking for a new word without the initial word being checked. So, you do actually need the IF statement inside the while loop.
This would be the way to do it, taking both yours and Yanuar's code in consideration
import java.io.Console;
public class TreeStory {
public static void main(String[] args) {
Console console = System.console();
/* Some terms:
noun - Person, place or thing
verb - An action
adjective - A description used to modify or describe a noun
Enter your amazing code here!
*/
String ageAsString = console.readLine("how old are you?");
String adjective;
int age = Integer.parseInt(ageAsString);
if (age < 13) {
console.printf("sorry you must be 13\n");
System.exit(0);
}
String name = console.readLine("Enter your name: ");
adjective = console.readLine("Enter an adjective: ");
do {
// you actually DO need an IF statement here so that you don't assume that the entered word is bad
// right from the start. If it isn't a bad word, nothing will happen inside this while loop
// and the program continues executing with printing the story
if (adjective.equalsIgnoreCase("gay") || adjective.equalsIgnoreCase("bitch")) {
adjective = console.readLine("do it again, you maker of coitus, you%n");
}
} while (adjective.equalsIgnoreCase("gay") || adjective.equalsIgnoreCase("bitch"));
console.printf("%s is very %s%n", name, adjective);
}
}
You could go without an if statement inside the while loop if it's actually a plain while loop (not a do while loop), so that the loop is executed only if we in fact have a bad word entered.
like so
// this is in place of the previous do { ... } while (); code
while (adjective.equalsIgnoreCase("gay") || adjective.equalsIgnoreCase("bitch"))
{
adjective = console.readLine("do it again, you maker of coitus, you%n");
}
And it's arguably cleaner and easier to read as well.
Yanuar Prakoso
15,197 PointsAhh... My bad.. Boban is right, you do need IF statement if you are using DO WHILE loop. I forgot that the DO will be run first by the compiler then it will test using the WHILE later.
You should use the WHILE loop though it is much cleaner.
I am sorry
Yanuar Prakoso
15,197 PointsHi Derek
the main problem is you forget to give the user the chance to change his or her adjective. Therefore it keeps looping since the adjective will always be = "gay" || "bitch".
And you also making mistakes on putting the console.printf("do it again fucker" outside the IF statement {...}
On the other hand you do not need this IF statement. since there is no changes in statement.
This is my proposed code:
import java.io.Console;
public class TreeStory {
public static void main(String[] args) {
Console console = System.console();
/* Some terms:
noun - Person, place or thing
verb - An action
adjective - A description used to modify or describe a noun
Enter your amazing code here!
*/
String ageAsString = console.readLine("how old are you?");
String adjective;
int age = Integer.parseInt(ageAsString);
if (age < 13){
console.printf("sorry you must be 13\n");
System.exit(0);
}
String name = console.readLine("Enter your name: ");
adjective = console.readLine("Enter an adjective: ");
do{
//you do not need IF statements just staright:
console.printf("do it again fucker%n");
adjective = console.readLine("re-enter your adjective: ");//<-- give your users to change their adjective
}while(adjective.equalsIgnoreCase("gay") || adjective.equalsIgnoreCase("bitch"));
console.printf("%s is very %s%n", name, adjective);
}
}
I hope this help a little.
Boban Talevski
24,793 PointsBoban Talevski
24,793 PointsWhat's the problem you are getting? Not sure your code should go uncensored btw :).
Also try to update the code markup, i.e. put all the code within the backticks, and after the opening backticks add java on the same line so it gets proper highlighting and is easier to read.