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

Chris Ayers
Chris Ayers
554 Points

Question on this problem

I am having trouble on what this is really wanting. I know it has to return an int which would be the count of each line, but am I suppose to make an array or sting to go through to check each name to see if it goes in line one or two?
My code so far is with this, it is not right but it is all I could think of right now, and was not sure if I am thinking correctly on this problem.
Feel free to change anything that I put, I think it would be best to know what mistakes I am making and what is a better route to take for this problem!

Thanks

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 */
    String[] myStringArray = {"A","B","C","D","E","F","G","H","I","J","K","L","M"};
    int lineOne = 0;
    int lineTwo = 0;
  for (int i = 0; i > myStringArray.length(); i++){
    if (leastName.charAt(0) == myStringArray.charAt(i){
      lineOne++;
    }
    else{
      lineTwo++;
    }
  }
    return lineOne;
    return lineTwo;


  }

}
Yonatan Schultz
Yonatan Schultz
12,045 Points

There's a lot going on here. You have a char (the first letter of lastName) and an array (myStringArray). You should be able to remove all of

for (int i = 0; i > myStringArray.length(); i++){ if (leastName.charAt(0) == myStringArray.charAt(i){ lineOne++; } else{ lineTwo++; }

And replace it with a simple call to the contains() method similar to https://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value

Then you have a simple if char is in my array, lineOne, otherwise lineTwo

I hope that that helps!

1 Answer

Edward C. Young
Edward C. Young
10,323 Points

Try

if (mLastName.charAt(0) > 'M' {
    // Do something with line2... Code Here
   return line2;
  }
else {
     // Do something with line1... Code Here
    return line1;
}

Chars are actually ints. M happens to be 77. See: Capital letter M , so what java actully compares is:

if(charAt(0) > int 77) { 
   //that's every letter greater than M, up to 90, which is Z.
}

There's no need to test each one, and you can reverse the if even:

if (mLastName.charAt(0) < 'M' {
    // Do something with line1... Code Here
   return line1;
  }
else {
     // Do something with line2... Code Here
    return line2;
}
Chris Ayers
Chris Ayers
554 Points

Thanks both solutions given helped me out a lot, I forgot about letters having an actual number value to them!

Chris Ayers
Chris Ayers
554 Points

Thanks again for the help, finally got it, this is what I put: private String mLastName; private int line1 = 1; private int line2 = 2; private boolean isTrue;

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
     */
    mLastName = lastName;
    isTrue = true;
    if (mLastName.charAt(0) <= 'M' && isTrue == true) {
        return line1;
    } else {
        isTrue = false;
        return line2;
    }
}

}

I had to do a lot of trial and error because I was a little confused on what the question really wanted for an answer! lol