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

Guys I need your help with the if statement. What is going wrong?

Initially, I thought it was my quotations over the secondExample but now I am not so sure.

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 equal to second".);
      System.exit(0);
}

String secondExample = "hello";
String thirdExample = "HELLO";

2 Answers

There are a few things:

  • Your code should go after the variable declarations
  • You have a period after "first is equal to second"
  • You won't need this line: System.exit(0);
Maxwell Newberry
Maxwell Newberry
7,693 Points

In your code, the order in which the compiler reads your code is from top to bottom. In your code, you have initialized the variable firstExample and follow it with your if-statement. But in your condition, you are checking if firstExample is equal to secondExample, however, you have not yet initialized secondExample up to this point. So referencing it would lead to errors. So we want to first change our code around so our if-statement goes AFTER the preset variables.

The next issue is that after your if statement, you ended that line with a semi-colon. This ends the statement and does not allow the compiler to run any further. Make sure you remove that semi-colon.

Next, in your print statement, you accidentally added a period at the end of your print statement

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

Make sure you remove that.

Finally, you have System.exit(0); -- which is not needed in this exercise.

Our final code comes to be:

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