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 trialAmin Malik
2,059 PointsComparing Characters in Java
I've been trying to compare characters within Java with =< and >= but I'm not sure if I'm doing it correctly and the editor isn't giving me any feedback. My question is whether or not I've properly created a char called order and whether I compared it to another character correctly. My code is below and what I'm specifically asking about is whether lines 8 and 9 are written correctly. Please feel free to point out any other mistakes I may be making. Thanks in advance for your help I truly appreciate it.
public class ConferenceRegistrationAssistant {
public int getLineFor() {
/* If the last name is between A thru M send them to line 1
Otherwise send them to line 2 */
Console console = System.console();
String lastName = console.readLine("Enter your last name: ");
char order = lastName.charAt(0);
if (order =< 'M') {
int line = 1;
} else {
int line = 2;
}
return line;
}
}
6 Answers
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHello I think the problem is int line = 2; should just read line = 2; once a variable is declared you don't need the type cast.
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHello, instead of creating a variable it might be easier to just compare it in the if statement. Such if (lastName.charAt(0) <= 'M') { // do stuff } Also, less than or equal to is <=.
You have also declared your variable twice. Your easiest way is to initally declare it it 0 before the loop. Then have it change during the loop.
It also looked like you imported and created a console, assume that there is already hidden code that we cannot see that will pass in a value for last name, so there's no need to make code that will be able to read it.
The beginning code should look like.
int line = 0;
```if (lastName.charAt(0) <= 'M') { //do stuff
} else {
do stuff
}
return line;
}```
Sam Kwok
2,448 PointsRob is correct. You've already define the line variable (int line = 0;). In your if/else block, you just want to assign the line variable a value depending on the conditions . By adding the "int" again, you are trying to redefine the same variable. I hope that helps.
Amin Malik
2,059 PointsThanks a lot for your help Rob. I really appreciate it. I still haven't been able to solve the exercise but your guidance is still very helpful. Thanks again.
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHello. Can you copy your updated code and I'll take a look at it?
Amin Malik
2,059 PointsThis is the code I wrote after taking into account your advice. However, I keep getting a compiler error but the preview isn't showing me what the error is (it's just a blank black page). I am curious as to whether or not anyone else knows what it is I am doing wrong. Thanks in advance for the help. int line = 0; if (lastName.charAt(0) <= 'M') { int line = 1; } else { int line = 2; } return line; }
Amin Malik
2,059 PointsThank you both for your help. Trying to redefine the same variable was exactly what I was doing wrong before. I appreciate you guys assisting me.