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

Ben Taylor
Ben Taylor
354 Points

i dont understand how to fix this if (firstExample == thirdExample.equalsIgnoreCase()) { printf("first and ....}

i dont understand how to fox this to check whether the first ad third statement are the same regardless of case

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 == thirdExample.equalsIgnoreCase()) {
  printf("first and third are the same ignoring case");
}

1 Answer

Hi,

I would do something like this:

if (secondExample.equals(firstExample)) {
  console.printf("first is equal to second");
}
if (thirdExample.equalsIgnoreCase(firstExample)) {
  console.printf("first and third are the same ignoring case");
}

When you do string equality tests between two strings (let's say str1 and str2) you don't do str1 == str2; you should call the equals() method on one of the strings and pass the other string as input argument, like str1.equals(str2).

Hope this helps.

Ben Taylor
Ben Taylor
354 Points

thankyou, this was very helpful

Andrei's solution is almost perfect, but you must include the "console" before "printf", like so:

if (secondExample.equals(firstExample)) { console.printf("first is equal to second"); } if (thirdExample.equalsIgnoreCase(firstExample)) { console.printf("first and third are the same ignoring case"); }

Thanks for spotting that. Fixed it.