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 trialChris Ayers
554 PointsQuestion 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
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;
}
}
1 Answer
Edward C. Young
10,323 PointsTry
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
554 PointsThanks both solutions given helped me out a lot, I forgot about letters having an actual number value to them!
Chris Ayers
554 PointsThanks 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
Yonatan Schultz
12,045 PointsYonatan Schultz
12,045 PointsThere'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-valueThen you have a simple if char is in my array, lineOne, otherwise lineTwo
I hope that that helps!