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

Add an if statement that checks to see if firstExample is equal to secondExample. If it is, print out "first is equal to

I tried typing "hello" the first time but that didn't work either. Help please.

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

4 Answers

The conditional statement is expecting firstExample to equal literally "secondExample" you set the variable in a set of strings therefore it'll be expecting that. FIx

// I have imported a java.io.Console for you, itis named console. 
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if (firstExample.equalsIgnoreCase (secondExample)) {
  console.printf("first is equal to second. \n");
  system.exit(0);
}

If you have no strings in the parentheses then it will know that your talking about the string secondExample as opposed to literally expecting secondExample as the value of firstExample.

To be specific if you didn't have the set of strings on secondExample it will now replace secondExample with

if (firstExample.equalsIgnoreCase ("hello")) {
  console.printf("first is equal to second. \n");
  system.exit(0);
}

if (firstExample.equals(secondExample)) { console.printf("first is equal to second"); }

I thank you for your time however, it still shows as incorrect according to the automated system. // I have imported a java.io.Console for you, it is named console. String firstExample = "hello"; String secondExample = "hello"; String thirdExample = "HELLO";

if (firstExample.equalsIgnoreCase(secondExample)) { console.printf("first is equal to second. \n"); system.exit(0); }

system.exit(0) is not needed :)

Thomas Salai
Thomas Salai
4,964 Points

What wrong with my code here.?

// 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 equal");
}

I got an error.

Bummer! There is a compiler error. Please click on preview to view your syntax errors!
Thomas Salai
Thomas Salai
4,964 Points

I found the syntax error.. in if statement.. I forgot the end ")".