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

Bobby Stanton
Bobby Stanton
5,776 Points

Everything seems right. Even went to questions to check and it looks right. But still get "bummer"!

// I have imported a java.io.Console for you, it is named console.

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

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

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Bobby,

All languages including Java; read from top-to-bottom, what this means is if a variable doesn't exist in the compilers memory yet, it won't know anything about it, therefore an error will be thrown. This is the case with the code above as you've declared your IF statement before secondExample instead of after it which results in the below error.

JavaTester.java:106: error: cannot find symbol
if (firstExample.equals(secondExample)) {
                        ^
  symbol:   variable secondExample
  location: class JavaTester
1 error

To resolve this, you simply need to move the IF statement further down to where secondExample has been declared and parsed by the compiler.

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

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

Happy coding!

Bobby Stanton
Bobby Stanton
5,776 Points

Hey, Chris! Thanks very much. That worked perfectly. I can't believe (nor do I want to say!! ;-) ) how long I struggled with this one!!! Thank you, Bobby