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

Greg Records
Greg Records
2,060 Points

I'm not sure how to compare two strings from watching this lesson.

The lesson showed how to match a string to a preset word but not to another string in the code. doing a simple

if (string = string)

was not accepted and

if (String.equals(String)){ }

Was not either.

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

Strange i'm doing this:

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

and its failing but no errors are listed in the preview. Any chance this challenge is broken at the moment. I checked online and my lords and masters at stack overflow are saying I'm doing it right:

http://stackoverflow.com/questions/5307074/how-to-compare-two-strings-are-equal-in-value-what-is-the-best-method

Craig Dennis
Craig Dennis
Treehouse Teacher

I see a capital E on System.exit and that will definitely cause problems. Can you send your current code again please?

Greg Records
Greg Records
2,060 Points
String firstExample = "hello";
String secondExample = "hello";
if (firstExample.equals(secondExample)){
  console.printf("first is equal to second");
  System.exit(0);
  }

even with a lowercase E on System.exit it fails with no error codes.

Craig Dennis
Craig Dennis
Treehouse Teacher

Now that we've got that solved....why are you calling System.exit? I think the main problem this isn't succeeding is because, it is exiting ;)

Hope that helps!

Greg Records
Greg Records
2,060 Points

That fixed it! Oh and I was just calling system exit because it was used in the lesson. I'm seeing that it isn't necessary for this challenge though.

Thanks!