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 Creating the MVP Conference Registration Assistant

Andrei Oprescu
Andrei Oprescu
9,547 Points

The next question.

I have a question like this:

Fix the getLineNumberFor method to return a 1 if the first character of lastName is between A and M or else return 2 if it is between N and Z.

And I have this code:

And when I check, it gives me this:

public int getLineNumberFor(String lastName) { int lineNumber = 0; int first = 1; int second = 2; String line1 = "a,b,c,d,e,f,g,h,i,j,k,l,m"; char letter = lastName.charAt(0); boolean half = line1.indexOf(letter) != 1; if (half) { lineNumber = first; } else { lineNumber = second; } return lineNumber; }

Bummer! Hmmm, I entered "Nelson" for the lastName and expected line number 2 to be returned, but instead it was 1.

I have checked this code a dozed times but I am still confused what the problem is.

Colud you please report this problem?

Andrei.

ConferenceRegistrationAssistant.java
public class ConferenceRegistrationAssistant {

  /**
   * Assists in guiding people to the proper line based on their last name.
   *
   * @param lastName The person's last name
   * @return The line number based on the first letter of lastName
   */
 public int getLineNumberFor(String lastName) { 
   int lineNumber = 0;
   int first = 1; 
   int second = 2; 
   String line1 = "a,b,c,d,e,f,g,h,i,j,k,l,m";
   char letter = lastName.charAt(0);
   boolean half = line1.indexOf(letter) != 1;
   if (half) {
     lineNumber = first;
   } else { 
     lineNumber = second;
   } 
   return lineNumber;
 } 

}
    /*
      lineNumber should be set based on the first character of the person's last name
      Line 1 - A thru M
      Line 2 - N thru Z

     */
Example.java
public class Example {

  public static void main(String[] args) {
    /*
      IMPORTANT:  You can compare characters using <, >. <=, >= and == just like numbers
     */
    if ('C' < 'D') {
      System.out.println("C comes before D");
    }

    if ('B' > 'A') {
      System.out.println("B comes after A");
    }

    if ('E' >= 'E') {
      System.out.println("E is equal to or comes after E");
    }

    // This code is here for demonstration purposes only...
    ConferenceRegistrationAssistant assistant = new ConferenceRegistrationAssistant();
    /*
      Remember that there are 2 lines.
      Line #1 is for A-M
      Line #2 is for N-Z
     */
    int lineNumber = 0;
    /*
      This should set lineNumber to 2 because
      The last name is Zimmerman which starts with a Z.
      Therefore it is between N-Z
     */
    lineNumber = assistant.getLineNumberFor("Zimmerman");

    /*
      This method call should set lineNumber to 1, because 'A' from "Anderson" is between A-M.
     */
    lineNumber = assistant.getLineNumberFor("Anderson");

    /*
      Likewise Charlie Brown's 'B' is between 'A' and 'M', so lineNumber should be set to 1
     */
    lineNumber = assistant.getLineNumberFor("Brown");
  }

}

2 Answers

andren
andren
28,558 Points

There are two issues with your solution:

  1. Case matters, N and n are not the same letter as far as Java is concerned. Since your line1 variable is composed of lower case letters it won't work when a capital letter is passed in.
  2. If you want to see if a letter is not present using the indexOf method then you need to compare it to -1 not 1 like you are doing in your code:

If you fix both of those issues like this:

public class ConferenceRegistrationAssistant {

  /**
   * Assists in guiding people to the proper line based on their last name.
   *
   * @param lastName The person's last name
   * @return The line number based on the first letter of lastName
   */
 public int getLineNumberFor(String lastName) { 
   int lineNumber = 0;
   int first = 1; 
   int second = 2; 
   String line1 = "A,B,C,D,E,F,G,H,I,J,K,L,M"; // Store capital letters
   char letter = lastName.charAt(0);
   boolean half = line1.indexOf(letter) != -1; // Compare against -1
   if (half) {
     lineNumber = first;
   } else { 
     lineNumber = second;
   } 
   return lineNumber;
 } 
}

Then your code will work. But it is worth noting that while your solution works it is a lot more complex than it needs to be. As the examples in the Example.java file illustrates you can actually compare letters against each other. Which means that you can solve the task just using this code:

public class ConferenceRegistrationAssistant {

  /**
   * Assists in guiding people to the proper line based on their last name.
   *
   * @param lastName The person's last name
   * @return The line number based on the first letter of lastName
   */
  public int getLineNumberFor(String lastName) {
    int lineNumber = 0;
    if (lastName.charAt(0) <= 'M') {
      lineNumber = 1;
    } else {
      lineNumber = 2;
    }
    return lineNumber;
  }

}
Andrei Oprescu
Andrei Oprescu
9,547 Points

Thank you.

I have understood my mistake.