Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Perfecting the Prototype String Equality

Joshua Salas
Joshua Salas
732 Points

Trying 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

Equality.java
// 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

Hi 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.

Joshua Salas
Joshua Salas
732 Points
// I have imported a java.io.Console for you, it is named console. 
String firstExample = "hello";
String secondExample = "hello";
  if(firstExample.equals(secondExample)){
  console.printf("first is equal to second");
  System.exit(0);
}
String thirdExample = "HELLO";

I made the changes to my code and still no luck getting past this excercise

Joshua Salas
Joshua Salas
732 Points
// 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");
  System.exit(0);
}

I've even tried it like this

Just checked and it is the System.exit(0); line that's causing your problem. Omit that and your code is fine.

Sorry - 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