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
Shannon Ferguson
4,307 PointsExtra credit feedback- MadLibs
Hi there I have completed the java objects section and attempted the extra credit - using objects to recreate MadLibs. I have posted it below. Is it possible to get any feedback from anyone? I am wondering if I went about it in a good way? None of my methods return anything, so it is not necessarily complex in any way, but it works.
I look forward to any response.
Thanks
Shannon
public class TreeStory {
public static void main(String[] args) {
Game game = new Game();
game.Intro();
game.InvalidWords();
game.Prompt();
}
}
import java.io.Console;
public class Game
{
Console console;
private String mName;
private String mAdjective;
private String mVerb;
public Game()
{
console = System.console();
}
public void Intro()
{
String ageAsString = console.readLine("How old are you? ");
int age = Integer.parseInt(ageAsString);
if(age < 13)
{
console.printf("Sorry you must be 13 Bra!");
System.exit(0);
}
mName = console.readLine("Enter your name: ");
}
public void InvalidWords()
{
boolean isInvalidWord;
do {
mAdjective = console.readLine("Enter your adjective: ");
//equalsIgnoreCase Compares this String to another String, ignoring case considerations.
isInvalidWord = (mAdjective.equalsIgnoreCase("dork") ||
mAdjective.equalsIgnoreCase("jerk"));
if(isInvalidWord)
{
console.printf("That language is not allowed Jedi Student! Try again! \n\n");
}
}while(isInvalidWord);
}
public void Prompt()
{
mVerb = console.readLine("Enter your verb: ");
console.printf("%s is very %s and likes to %s a lot\n", mName, mAdjective, mVerb);
console.printf("she like to %s at night, which is %s\n",mVerb, mAdjective);
}
}
3 Answers
Ken Alger
Treehouse TeacherShannon;
Overall it looks fairly decent. There is a bit of repetition in your code to check for the InvalidWords though. You could DRY that up a bit with something along the lines of...
String noun;
Boolean isInvalidWord;
String word = "nerd dork jerk lame";
do {
noun = console.readLine("Enter a noun: ");
//isInvalidWord = badWords.contains(noun.toLowerCase());
isInvalidWord = containsIgnoreCase(noun, word);
if (isInvalidWord) {
console.printf("That language is not allowed! Try again. \n\n");
}
} while(isInvalidWord);
// Do something amazing here
This would allow you to only have to change your word list in one spot. The containsIgnoreCase method works pretty well for this extra credit assignment.
Happy coding,
Ken
Shannon Ferguson
4,307 PointsThank you Ken. Much appreciated.
Shannon Ferguson
4,307 PointsI am not sure why but I can't get this to work. It keeps saying:
Game.java:43: error: cannot find symbol
isInvalidWord = containsIgnoreCase(noun, word);
^
symbol: method containsIgnoreCase(String,String)
Do I need to use dot syntax before containsIgnoreCase?
Ken Alger
Treehouse TeacherShannon;
Did you define isInvalidWord?
Post your code and let's see what's up.
Ken
Shannon Ferguson
4,307 PointsHey Ken
Here is the code block giving an error:
public void InvalidWords() { String noun; boolean isInvalidWord; String word = "nerd dork jerk lame"; do { noun = console.readLine("Enter your adjective: ");
isInvalidWord = containsIgnoreCase(noun, word);
if(isInvalidWord)
{
console.printf("That language is not allowed Jedi Student! Try again! \n\n");
}
} while(isInvalidWord);
}
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherEdited for markdown