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

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,650 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