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 trialGaurab Biswas
668 PointsIn the code challenge exercise I am stuck. String secondExample
The code challenge is: add an if statement that checks if the firstExample equals to secondExample and if it is equal it prints a line as mentioned in printf.
I am getting error. Please let me know where I am wrong.
// I have imported a java.io.Console for you, it is named console. String firstExample = "hello"; String secondExample = "hello"; String thirdExample = "HELLO"; if (secondExample.equals("hello")) { console.printf("first is equal to second."); System.exit(0); }
// I have imported a java.io.Console for you, it is named console.
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if (secondExample.equals("hello")) {
console.printf("first is equal to second.");
System.exit(0);
}
4 Answers
Steve Hunter
57,712 PointsHi Gaurab,
I missed that; apologies. You should be comparing the variables themselves:
if (firstExample.equals(secondExample)) {
console.printf("first is equal to second.");
}
Does that make sense?
Testing against the string itself works for the challenge (it probably shoudln't!) but that's not what we're trying to achieve. We want to see if the string stored in the two variables is the same.
I hope that helps.
Steve.
Steve Hunter
57,712 PointsHi there,
You don't need to have the System.exit
line in there. Delete that, and you'll be fine.
Steve.
Gaurab Biswas
668 PointsHi Steve,
should I write the condition -
if (secondExample.equals("hello")) { console.printf("first is equal to second."); }
or
if (firstExample.equals("hello")) { console.printf("first is equal to second."); }
or
both are logically correct?
Gaurab Biswas
668 PointsHi Steve,
Thanks for your reply.
I was doing the same at first but probably due to the line System.exit(0) I was getting error.
Then I started comparing with the string itself instead of variable.
I guess as you said, I should always compare variables themselves instead of string.
Thanks again for your reply.
- Gaurab.
Steve Hunter
57,712 PointsYes, the exit line would throw an error as the code would exit unexpectedly, even though the outputs would have been correct. Only use an exit command if you really need to interrupt code execution. If the code works fine as it is, but you run out of lines, that's fine; the code will just terminate gracefully - there's no need to force the halt.