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

Amin Malik
Amin Malik
2,059 Points

Comparing 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.

ConferenceRegistrationAssistant.java
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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hello, 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
Sam Kwok
2,448 Points

Rob 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
Amin Malik
2,059 Points

Thanks 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.

Amin Malik
Amin Malik
2,059 Points

This 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
Amin Malik
2,059 Points

Thank 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.