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

Kevin Jervis
Kevin Jervis
2,600 Points

Syntax errors

Hi I keep getting syntax errors. Not too sure why :o(

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

2 Answers

michaelcodes
michaelcodes
5,604 Points

Hi there! So there are two things that I noticed in your code here. The line:

if (firstExample equals("hello") 

There is a missing closing ) on the end, and their should be a period between the firstExample and equals. The reason for this is because you are calling the method "equals". Whenever you call a method you put a period between:

if (firstExample.equals("hello") )

All put together it should look like this:

String firstExample = "hello";
String secondExample = "hello";
if (firstExample.equals("hello") )
    {
  console.printf("first is equal to second.\n");
    }

Edit: Also for use in the future you will want to check the actual variable, rather than the string "hello" as so:

if (firstExample.equals(secondExample) )

It is easy to forget a ) or } or ; it happens all the time to even very experienced programmers!

Hope this helps!

If this was confusing or you have any additional questions don't hesitate to ask and I will respond the best I can

Kevin Jervis
Kevin Jervis
2,600 Points

Thanks again Michael. Always something really simple. All part of the learning process :o)

michaelcodes
michaelcodes
5,604 Points

Glad I could help! The learning process is easier for us all with community :)