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

It says compile error. if (firstExample.equals("secondExample")) {console.printf("first is equals to second");

This is the "Challenge Task" 1 of 2 question.

After I wrote the following, it says compile error.

Could you kindly advise which part I have done wrong?

String firstExample = "hello"; if (firstExample.equals("secondExample")) {console.printf("first is equals to second"); System.exit(0);} String secondExample = "hello";

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

1 Answer

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Zixi

First of all in your IF statement you should not use double quotes ("secondExample") because you want to compare with the value inside variable called secondExample which = "hello". Then you must put your IF statement below the String secondExample since you want to compare it with firstExample. Therefore the compiler needs to know what is secondExample is. Better yet for best practice you write your IF statement after String thirdExample.

And then in the task you only required to print ("first is equals to second"). You do not need to add System.exit(0)

I retry your challenge and here is what I came up with:

// 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(secondExample)) {
  console.printf("first is equals to second");
}

I hope this can help

Yanuar,

Many thanks for the detail guide. I really appreciate for your help!

Zixi