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

Mohammad Saeed
PLUS
Mohammad Saeed
Courses Plus Student 315 Points

i wrote if (firstExample.equals(secondExample)) { //wrote the rest of the block right } What am i doing wrong here?

It says try again, but is my code correct?

Equality.java
// 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\n");
  System.exit(0);
}
String thirdExample = "HELLO";

2 Answers

Mohammad Saeed
Mohammad Saeed
Courses Plus Student 315 Points

Thank you that worked! But what i don't get is, in the tutorial it was instructed to place System.exit(0); afterwards. So i'm guess because it wasn't instructed to automatically exit the program that isn't needed then?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Mohammed Saeed Keep in mind that challenges are a little special. They have to be able to evaluate your code. My guess is here that if the system exits... well... they can't evaluate it anymore.

:information_source: It's a good idea (inside of challenges) to never do anything they don't explicitly ask for. Try not to implement variables they don't want etc. Even though the code may be fully functional outside the challenge, it can cause the challenge to fail.

I just did the challenge and passed the first task with this code:

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

Yes, most of the time you just add specifically what you are told to. It usually gives you a hint whether it is the compiler throwing the error or the challenge just telling you that the code/answer received isn't what was expected.