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 trialJoshua Salas
732 PointsTrying to compare the first string and second string using equals. It complies but says im not using it right .
Any ideas where i am going wrong or what i should do next
// I have imported a java.io.Console for you, it is named console.
String firstExample = "hello";
if(firstExample.equalsIgnoreCase("secondExample")){
console.printf("first is equal to second");
System.exit(0);
}
String secondExample = "hello";
String thirdExample = "HELLO";
2 Answers
Steve Hunter
57,712 PointsHi Joshua,
You've got two things a little wrong here.
First, you're comparing "secondExample"
, in quotes. You don't want to do that, you want to compare the contents of the variable called secondExample
, so remove the quotes.
Next, you've tried to do this before secondExample
has been declared. Move all your code to the bottom - add new lines for it.
Third, and I don't know if the compiler will pick up on this, but in the first task, you're supposed to be using .equals()
rather than .equalsIgnoreCase()
but that might not matter.
Good luck!
Steve.
Steve Hunter
57,712 PointsSorry - missed that - remove the System.exit(0)
line - you don't want the program to finish executing there.
Also, for the last task, make sure you write beneath the place where thirdExample
is declared.
String firstExample = "hello";
String secondExample = "hello";
if(firstExample.equals(secondExample)){
console.printf("first is equal to second");
}
String thirdExample = "HELLO";
// task 2 code here
Joshua Salas
732 PointsJoshua Salas
732 PointsI made the changes to my code and still no luck getting past this excercise
Joshua Salas
732 PointsJoshua Salas
732 PointsI've even tried it like this
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsJust checked and it is the
System.exit(0);
line that's causing your problem. Omit that and your code is fine.