1

I am implementing a form of leftist min heap, which stores arbitrary words by length. So, I have written a wrapper class for Scanner, and changed the compareTo, like so

public class ScannerWrapper implements Comparable<String>

//a Scanner, sc and a String, current
public int compareTo(String str){
    if(current.length() > str.length()) return -1;
    if(current.length() > str.length()) return 1;
    else return 0;
}

where current = sc.next() and is not the \n character.

in this case, if I have ScannerWrapper.next() > foo , where foo is an arbitrary string of length > ScannerWrapper.next();
will it use the compareTo(String) that I have written, returning false, or will it do some other random thing?

Sam P
  • 453
  • 6
  • 19
  • Sorry if my explanation seems muddled. Let my try to clarify - `String n = ScannerWrapper.next() and n.length() > foo.length() – Sam P Apr 05 '12 at 04:21
  • 1
    I can't really understand your question too well, but both of your `if` statement conditions are the same... – AusCBloke Apr 05 '12 at 04:26
  • Did you mean to use a `<` in the second comparison in the compareTo method? – Raze Apr 05 '12 at 05:28
  • It has nothing to do with the format of the code, assume it is correct as I never use exact transcripts of my code. The second if statement is supposed to be <. I'm asking whether or not the length of a given string length ( i < n ) given ( n > i ) will return -1 and i will be the head of a leftist min-heap – Sam P Apr 05 '12 at 12:42

2 Answers2

0

After reading your question several times I think I understand what you're asking now. If you're trying to compare two instances of class ScannerWrapper with the comparison operators, then no, it's not going to work.

You can't overload operators in Java (you can in C++), therefore in order to compare instances of ScannerWrapper with each other you're going to have to call the compareTo() method.

Also, both of your if statement conditions are the same, so you might want to fix that up.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
  • I never use exact transcripts of my code on here, I try to make them as general as possible. As is the case, missights like the if statements are possible, thank you for noticing. So, I should be comparing two instances of the class ScannerWrapper? Say I have a string length N that is the next word in the stream, and a string length i < N in my min-heap. Therefore, i should be position 0 on the min-heap, and N should be 2, and so-on. Is this how I would implement? – Sam P Apr 05 '12 at 12:38
  • @SamP: You can compare a `ScannerWrapper` and a `String` or two `ScannerWrapper`s, it's up to you and how you code your overloaded `compareTo()` methods. You would use `compareTo()` to determine where in the min-heap a current value should go, but I'm guessing your heap would store either `String`s or `ScannerWrapper`s so I'd probably expect two of the same type of objects being compared to each other. – AusCBloke Apr 05 '12 at 22:06
  • The min-heap would store strings, so perhaps I should write a class that wraps up a string and have a seperate scanner? – Sam P Apr 06 '12 at 09:52
0

It's difficult to understand your question - so you might consider rephrasing it. Here's a shot in the dark :

 public class ScannerWrapper implements Comparable<ScannerWrapper>

    //your wrapper has a handle to the scanned data.   Presumably it's
    //initialized on construction, which is omitted here
    private final String scannedData;        

    public String getScannedData() { 
        return this.scannedData;
    }

    public int compareTo(ScannerWrapper other) {
        //if this scannedData is longer than the other, return 1
        if(this.str.length() > other.getStr().length()) {
            return 1;
        } else if(this.scannedData.length() < other.getScannedData().length()) {
        //if the other scannedData is longer return -1   
            return -1;
        }
        //if they are equal return 0
        return 0;
    }

 }
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124