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 Java Objects Creating the MVP Current Progress

Ranvir Sahota
Ranvir Sahota
9,844 Points

One line of code breaks hangman

Simply by having the display variable empty like so char display = ''; breaks the entire game and I received the following errors: Game.java:25: error: empty character literal
char display='';
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
./Game.java:25: error: empty character literal
char display='';
^
./Game.java:25: error: unclosed character literal
char display='';
^
./Game.java:25: error: ';' expected
char display='';
^
./Game.java:26: error: not a statement
if (hits.indexOf(letter) != -1) {
^
./Game.java:26: error: ';' expected
if (hits.indexOf(letter) != -1) {
^
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
./Game.java:25: error: empty character literal
char display='';
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
./Game.java:25: error: empty character literal
char display = '';
^
./Game.java:25: error: unclosed character literal
char display = '';
^
./Game.java:25: error: ';' expected
char display = '';
^
./Game.java:26: error: not a statement
if (hits.indexOf(letter) != -1) {
^
./Game.java:26: error: ';' expected
if (hits.indexOf(letter) != -1) {
^
5 errors

I have resolved the problem by putting in a hyphen as a default as stated in the video. But I would like to know why this breaks the game as I cannot understand why it produces the errors it does. For example: unclosed character literal when string is got two quotation marks one to open another to close. Snapshot of workspace : https://w.trhou.se/vptprogh5z

1 Answer

andren
andren
28,558 Points

It breaks the game because it is invalid code. In Java a char is a datatype that stores 1 character, not 0 or 1, exactly 1. That is how the datatype is defined and anything that does not conform to that will generate a compiler error.

Ranvir Sahota
Ranvir Sahota
9,844 Points

Thanks for the quick response andren. But I still don't quite fully understand why would it generate an error like error ';' unexpected when it has nothing to do with what you said? Surely it would make more sense to generate an error to describe that char must have a one character. I know I'm just being stupid by picking at something small but if I don't understand this it will just gnaw away at brain