0

I am trying to use compareTo method to compare two different names. After running the first attempt the program terminates immediately without returning anything. How can I modify this compareTo method to compare the names (Name n and Name n2) and return the result (-1, 1 or 0)? And obviously a print statement can be added to display (equal, before , or after) for the comparison. Thanks for any assistance.

//First attempt
public class Name implements Comparable<Name> {

    private String Name;

    public Name(String string) {
        // TODO Auto-generated constructor stub
    }
    public String getName() {
        return Name;
    }
    public int compareTo(Name other) {

        if (getName().compareTo(other.getName()) < 0) {
            return -1;
        } else if (getName().compareTo(other.getName()) > 0) {
            return 1;
        } else if (getName().equals(other.getName())) {
            return 0;
        }
        return getName().compareTo(other.getName());
    }
    public static void main(String[] args) {
        Name n = new Name("jennifer");
        n.getName();
        Name n2 = new Name("paul");
        n2.getName();
    }
}




//second attempt
    public class Name implements Comparable<String> {

        private String Name;

        public String getName() {
            return Name;
        }
        public int compareTo(String other) {

            if (getName().compareTo(other.getName()) < 0) {
                return -1;
            } else if (getName().compareTo(other.getName()) > 0) {
                return 1;
            } else if (getName().equals(other.getName())) {
                return 0;
            }
            return getName().compareTo(other.getName());

        }
        public static void main(String[] args) {
            String Name = new String("jennifer");

            String other = new String("paul");
        }
    }
Peanutcalota
  • 135
  • 1
  • 8
  • It's unclear what you're trying to do. And why don't you just `return getName().compareTo(other.getName())`? What are all the ifs for? – shmosel Feb 19 '16 at 07:13
  • The if's are to compare whether or not the two names (Strings) are equal (lexicographically equivalent), smaller - the String calling the method is lexicographically first (comes first in a dictionary), or larger (the parameter passed to the compareTo method is lexicographically first) . According to lexicographical order, which similar to the ordering that one might find in a dictionary. – Peanutcalota Feb 19 '16 at 07:25
  • You get that all out of the box by calling [`String.compareTo()`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)). – shmosel Feb 19 '16 at 07:28
  • Understood, just wanted to illustrate what's happening in the background. Thanks for the help and clarification! – Peanutcalota Feb 19 '16 at 07:40

2 Answers2

1
//First attempt
public class Name {

    public static void main(String[] args) {
        String n = new String("jennifer");
        String n2 = new String("paul");
        if (n.compareTo(n2) < 0) {
            System.out.println(n +" is before than " +n2);
        } else if (n.compareTo(n2) > 0) {
            System.out.println(n +" is after than " +n2);
        } else if (n.compareTo(n2) == 0) {
            System.out.println(n +" is equals to " +n);
        }
    }   
}

Outoput:

jennifer is before than paul

By the way, check this out because every programming language has its own set of rules and conventions and for variables in Java is like this:

If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word.

tumisma
  • 375
  • 3
  • 14
-1
public class Name implements Comparable<Name> {

    private String name;

    public Name(String name) {
        this.name=name;
    }
    public String getName() {
        return name;
    }
    public int compareTo(Name other) {

        if (getName().compareTo(other.getName()) < 0) {
            return -1;
        } else if (getName().compareTo(other.getName()) > 0) {
            return 1;
        } else if (getName().equals(other.getName())) {
            return 0;
        }
        return getName().compareTo(other.getName());
    }
    public static void main(String[] args) {
        Name n = new Name("jennifer");
        n.getName();
        Name n2 = new Name("paul");
        n2.getName();
       System.out.println(n.getName());
      System.out.println(n2.getName());
       System.out.println(n2.compareTo(n));

    }
}

OUTPUT : jennifer paul 1

P S M
  • 1,121
  • 12
  • 29