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?
It's difficult to understand your question - so you might consider rephrasing it. Here's a shot in the dark :
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 thecompareTo()
method.Also, both of your
if
statement conditions are the same, so you might want to fix that up.