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

Can we scan line for character directly or do we have to scan for string first?

We are currently doing this:

public boolean promptForGuess(){ Scanner scan = new Scanner(System.in); System.out.print("Guess a letter: "); String guessInput = scan.nextline(); char letter = guessInput.charAt(0); return game.applyGuess(letter); }

Is there a simpler method where we scan for a char, so that we don't have to define a new variable that is the first char of the entered string? Would this cause problems if a string were input?

1 Answer

Unfortunately there is no method on the Scanner class that reads a character. For example "scan.nextChar()". Assigning the first character of the inputted String is the best way (even though it's pretty ugly). If you want to read more about the Scanner class, check out : https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html