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

Jonathan Torres
Jonathan Torres
867 Points

How do I use "if" statement to compare two stings to see if they are equal.

I am trying to do the challenge for Equality java but when I try to compare the two strings the way I was shown how to in the video it comes out wrong. I am not sure where my error is. I looked up on google how to but could not find what I was looking for.

Equality.java
// I have imported a java.io.Console for you, it is named console. 
String firstExample = "hello";
String secondExample = "hello";
if (firstExample.equals (secondExemple));{
  console.printf("First is equal to second".);
}
String thirdExample = "HELLO";
Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Jonathan,

additionally to Jacobs advice I see a dot here

console.printf("First is equal to second".);

change it to

console.printf("First is equal to second");

and a typo in your secondExample variable:

if (firstExample.equals (secondExemple)){

change it to:

if (firstExample.equals (secondExample)){

So your code should look like this :

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

Keep on coding bro ....

Grigorij

1 Answer

Jacob Bergdahl
Jacob Bergdahl
29,119 Points

Hi! It looks like you accidentally put a semicolon on the fourth line (at the end of the first line of the if-statement). Try changing this "if (firstExample.equals (secondExemple));{" to this: "if (firstExample.equals (secondExemple)) {", and see if it helps!