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 Objects (Retired) Creating the MVP Comparing Characters

[HELP] I'm close...but not there yet...

When I run this piece of code, it doesn't give me any errors...except for one:

"I entered Anderson and expected to get back line 1 as A comes before M"

ConferenceRegistrationAssistant.java
public class ConferenceRegistrationAssistant {

  public int getLineFor(String lastName) {
    /* If the last name is between A thru M send them to line 1
       Otherwise send them to line 2 */
    if (lastName.charAt(0) >= 'M') {
      int line = 2;
    } else {
      int line = 1;
    }
    int line = 0;
    return line;
  }

}

2 Answers

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

You've nearly got it! You just need to change where you declare line. You need to declare line before the if statement, and get rid of the declaration of line after the if statement. Also, because you declare line before the if statement, you don't need to declare it in any of the blocks - you just need to write "line = 2" instead of "int line = 2", or "line = 1" instead of "int line = 1".

  public int getLineFor(String lastName) {
    /* If the last name is between A thru M send them to line 1
       Otherwise send them to line 2 */
    int line;
    if (lastName.charAt(0) >= 'M') {
      line = 2;
    } else {
      line = 1;
    }
    return line;
  }

I hope this helps!

Ah, that makes a lot of sense!

Thank you!

Jacob Martin
Jacob Martin
4,182 Points

Ok I figured it out. Looking at your if/else statement everything is correct, but looking afterwords you are declaring int line to be 0. So no matter what they put in your output of line will be 0.

public class ConferenceRegistrationAssistant {

  public int getLineFor(String lastName) {
    /* If the last name is between A thru M send them to line 1
       Otherwise send them to line 2 */
   int line = 0;

    if (lastName.charAt(0) >= 'M') {
       line = 2;
    } else {
      line = 1;
    }
    return line;
  }

}

A good way to check this for future reference is to use a printf or System.out.println(line) to see what the program is outputting.

It's giving me a compile error now

./ConferenceRegistrationAssistant.java:8: error: variable line is already defined in method getLineFor(String)
      int line = 1;
          ^
./ConferenceRegistrationAssistant.java:10: error: variable line is already defined in method getLineFor(String)
      int line = 2;
          ^
2 errors