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 trialZeeshan Somani
323 PointsCan someone tell me what is wrong with this code? I am trying this in the interactive session but cannot figure it out.
The point of this code is to output "first is equal to second if the contents of the 2 string are the same. Any ideas?
String firstExample = "hello"; if (firstExample.equals (secondExample)) { console.printf ("first is equal to second."); } String secondExample = "hello"; String thirdExample = "HELLO";
1 Answer
Rich Bagley
25,869 PointsHi Zeeshan,
The first part of the challenge should look like this:
// 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");
}
Is this what you meant?
-Rich
Zeeshan Somani
323 PointsZeeshan Somani
323 PointsThat is exactly what I wrote. The only difference is the placement. I placed the if statement between the first and second string. I ran a similar program in workshop so the method should work but the interactive question isn't accepting it. Is the placement at fault?
Rich Bagley
25,869 PointsRich Bagley
25,869 PointsHi Zeeshan,
Yes you will need to make sure your condition appears after the strings that are set. Think of it as though they can't be compared if they don't exist yet :)
Hope that helps.
-Rich
Zeeshan Somani
323 PointsZeeshan Somani
323 PointsPerfect. Works now. Thanks.
Rich Bagley
25,869 PointsRich Bagley
25,869 PointsNo problem :)
-Rich
Aamir Mirza
25,077 PointsAamir Mirza
25,077 PointsThanks Rich! I was having trouble with this too. I had the exact same code as you except I put System.exit(0); at the end of the if block. Hopefully that's a bug x_x