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

confused

I am confused as to where I am going wrong can someone review and give a few pointers

Equality.java
// I have imported a java.io.Console for you, it is named console. 
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if (firstExample == secondExample) {
  console.printf ("first is equal to second");
}
if (firstExample.equalsIgnoreCase ("HELLO"))
if (firstExample == thirdExample) {
  console.printf("first and third are the same ignoring case.");
}

2 Answers

Kevin Faust
Kevin Faust
15,353 Points

Hey Aven,

The problem lies here:

if (firstExample.equalsIgnoreCase ("HELLO")) // needs { } here 
if (firstExample == thirdExample) {
  console.printf("first and third are the same ignoring case.");
}

You have an if statement that is invalid. If statements always need a opening and closing brace { } however as i noted above, you can see you dont have one. In if statements, it will check if the statement inside the parantheses is true and then it will run the code block, but as you can see you have no code block so the program is confused and will throw an error

Also we want to check if the first example is equal to the third example by ignoring the case. You are comparing the firstExample to the thirdExample twice. We just need one if statement to do this. It should look like this:

if (firstExample.equalsIgnoreCase(thirdExample)) {
 console.printf("first and third are the same ignoring case"); 
}

it says "take the firstExample ("hello") and, ignoring cases, check if it equals the thirdExample ("HELLO")"

I hope that helped and you have a better understanding

If you have any other questions let me know! Dont forget to mark as best answer so others with the same question can see

Happy coding and all the best,

Kevin

Thank you Kevin it helped out a lot.