-2

I'm trying to compare the last names of two students in a directory, I'm doing this by overriding the compareTo method. The following code does not reflect exactly what I'm wanting to do here, the return value is just a placeholder for now. It is saying it won't compile because it cannot find the symbol for subString(int) in class String.

for (int i = 0; i < this.getLastName().length(); i++) {
     if (!this.getLastName().subString(i).equals(s.getLastName().subString(i))) {
        return 1;
     }
  }

Update: I appreciate y'all pointing out my idiocy in case sensitivity (no really, thanks) here's an update to where I'm at in the code. I think I can take it from here.

@Override
public int compareTo(Student s) {
  for (int i = 0; i < this.getLastName().length(); i++) {
     if (!this.getLastName().equals(s.getLastName())) {
        for(int j = 0; j < this.getLastName().length() || j < s.getLastName().length(); j++) {
           if (this.getLastName().charAt(j) < s.getLastName().charAt(j)) {
              return 1;
           }
           else {
              return -1;
           }
        }
     }

  }

looking at it again, I don't even need that first for loop.

and here's the finished method.

@Override
public int compareTo(Student s) {

  if (!this.getLastName().equals(s.getLastName())) {
     for(int j = 0; j < this.getLastName().length() || j < s.getLastName().length(); j++) {
        if (this.getLastName().charAt(j) < s.getLastName().charAt(j)) {
           return 1;
        }
        else if (this.getLastName().charAt(j) > s.getLastName().charAt(j)) {
           return -1;
        }
     }
  }

  if (!this.getFirstName().equals(s.getFirstName())) {
     for (int i = 0; i < this.getLastName().length() || i < s.getLastName().length(); i++) {
        if (this.getFirstName().charAt(i) < s.getFirstName().charAt(i)) {
           return 1;
        }
        else if (this.getFirstName().charAt(i) > s.getFirstName().charAt(i)) {
           return -1;
        }
     }
  }
  return 0;

}

Zach Thompson
  • 39
  • 2
  • 14
  • Try `substring` instead – imtheman Feb 04 '15 at 02:32
  • 4
    Java is case-sensitive. – PM 77-1 Feb 04 '15 at 02:32
  • Damn, I feel like an idiot now, thanks. – Zach Thompson Feb 04 '15 at 02:34
  • Also, if you are trying to compare each character of the string you would need to do `if(this.getLastName().charAt(i) != s.getLastName().charAt(i))` to get just a single character. Other wise you can just do `if(!this.getLastName().equals(s.getLastName()))` without a loop. – imtheman Feb 04 '15 at 02:35
  • This is NOT a duplicate of the classic "how do I compare Java strings" question. It is really about how to order records based on a pair of strings. Read the OP's code before you pull the trigger! – Stephen C Feb 04 '15 at 03:05

2 Answers2

0

The simple way to compare two Java strings is to use the String.compareTo(String). You don't need to implement that yourself using loops. (Or at least, not in a real world context.)

If you want to order by lastname then firstname:

call `compareTo` on the last name string 
 - if the result is non zero, return it
 - if the result is zero, compare the first name string.

I'll leave you to figure out the rest ... since this is clearly a "learning exercise".

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • You're right, sorry about that it was a progression problem where I couldn't figure out that subString was supposed to be substring. After that I pretty much had it. I put the end result up. – Zach Thompson Feb 04 '15 at 02:53
0

Not sure why you want to reinvent the wheel. Stephen C was trying to point out that you can achieve the same result like this:

@Override
public int compareTo(Student that) {
    int compare = this.getLastName().compareTo(that.getLastName());
    if (compare == 0) {
        compare = this.getFirstName().compareTo(that.getFirstName());
    }
    return compare;
}
gknicker
  • 5,509
  • 2
  • 25
  • 41