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 Basics Perfecting the Prototype String Equality

Sarat Chandra
Sarat Chandra
4,898 Points

Compiler Error! Help!

Here is the Compiler Error for the attached code.

JavaTester.java:108: error: ';' expected If (firstExample.equals("hello")) { ^ 1 error

It says ; is missing. I used the same method as the presenter used in the previous lesson. I don't know what I'm doing wrong. Please help!

Equality.java
// I have imported a java.io.Console for you, it is named console. 
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
If (firstExample.equals("hello")) {
  console.printf("first is equal to second");
  System.exit(0);
}

2 Answers

Tobias Mahnert
Tobias Mahnert
89,414 Points

Hi instead of using the String, you need to put in the variable name. Also you don"t need to put in the system exit method.

Check the code below for clarity. If you have any questions, please let me know.

if (firstExample == secondExample) {
 console.printf("first is equal to second");
}
if (firstExample.equalsIgnoreCase(thirdExample)) {
console.printf("first and third are the same ignoring case");
}
Sarat Chandra
Sarat Chandra
4,898 Points

Thanks for your answer. In the first if statement, I wanna use equals method rather than ==. Anyways, I ran my same code in the work space window and it worked. Guess there's some issue with the exercise compiler.

Thanks.

very much simplified.... thanks

Tobias Mahnert
Tobias Mahnert
89,414 Points

you can also use the equals operator but in order to pass the challenge you need to use the variable and not the value of the variable. Of course it works when you test it in the workspace but if you would use this code in the real world it would be inefficient to test against values instead of variables.

Sarat Chandra
Sarat Chandra
4,898 Points

Yeah got it :). I used variable in my code. I modified it later.