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

Java

How do i add an object that requires string input from the user in Hangman ? I tried a few ways not getting anywhere :/

The app we build during the course of Object in the Java track uses the args method for an input which u have to write on the command prompt "java Hangman 'the word u wanna be guessed ' " but i want to ask the user to input his word that he wants to guess, like he could give it to someone else to guess it. I tried with scanner and console.readLine tho i'm not getting anywhere, please some tips and advises would be really welcomed :)

Thank you

1 Answer

HI Enes,

The word that the player has to guess is passed into the Game constructor within the Hangman class. So, if you set up a scanner just before that to gather the user input then pass that string to Game that should work fine. Something like:

        Scanner scanner = new Scanner(System.in);
        System.out.printf("Enter your word to guess: ");
        String wordToGuess = scanner.nextLine();
        Game game = new Game (wordToGuess);

I hope that helps,

Steve.

Thanks a bunch Steve, i initially tried it like this but i used readLine instead of nextLine and well i just got confused and then since Game was taking the input i thought i had to create it in the class of Game. Thank you for helping me out :)

No problem! :+1: