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

Stéphanie Abdel Malek
Stéphanie Abdel Malek
453 Points

Incompatible type...

why is my code wrong?I changed the strings to int in order to be able to compare them...

Equality.java
// I have imported a java.io.Console for you, it is named console. 
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
int fstExample = Integer.parseInt(firstExample);
int scdExample = Integer.parseInt(secondExample);
if(fstExample = scdExample) {
  console.printf("first is equal to second");
  System.exit(0);
}

1 Answer

Mihai Craciun
Mihai Craciun
13,520 Points

There are several problems in your code First of all you don't have to convert the Strings to Integers. The task only ask you to write an if statement and check if the first string is equal second using .equals() method. So the code should look like this:

String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";

if(firstExample.equals(secondExample)){
  console.printf("first is equal to second");
}

EDIT: Why did you use the System.exit(0); method. This method is used just in special cases when you want to brute exit the program when a problem occurred.