Well, it's not so hard. At some point you read the line, where it still has the newline. Later you use that line in a comparison, at which point it shouldn't have the newline any more. So where do you need to remove it?
Also don't be afraid to just try stuff. It really helps.