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

String instead of char?

The video has this line of code;

String guessInput = scanner.nextLine();
char guess = guessInput.charAt(0);
return game.applyGuess(guess);

Would this not be better? if not why?

char guessInput = scanner.nextLine();
return game.applyGuess(guessInput);

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

this method scanner.nextLine() returns a String, hence, this value can't be assigned to guessInput if guessInput is a char. This has to do with the fact that Java is a strongly typed language, if a variable is assigned a new value, that new value needs to be of the same type w/ the variable, otherwise it's syntax error.