what error do you get with a compareTo method?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: what error do you get with a compareTo method?

  1. #1
    Join Date
    Mar 2005
    Posts
    6

    what error do you get with a compareTo method?

    --what would happen if you had an input of java MyDriver "candy apple red" candy apple red ?

    public class MyDriver {

    public static void main(String[] args) {

    Comparable t1 = new Truffle(args[0]);

    Comparable t2 = new MooseMunch(args[1]);

    if (t1.compareTo(t2.toString()) < 0)

    System.out.println(t1.toString());

    else if (t1.compareTo(t2.toString()) > 0)

    System.out.println(t2.toString());

    else

    System.out.println("1st = " + t1.toString() +

    " : 2nd = " + t2.toString());

    } // main

    } // MyDriver

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    How can we know as long as you have not posted the code for Truffle
    and MooseMunch ?
    eschew obfuscation

  3. #3
    Join Date
    Mar 2005
    Posts
    6

    here is the truffle class...

    Public class Truffle implements Comparable {

    private String flavor;

    public Truffle (String flavor) {

    this.flavor = flavor;

    }

    public int compareTo(Object o) {

    return this.flavor.compareTo((String) o);

    }

    public String toString() {

    return this.flavor;

    }

    } // Truffle

  4. #4
    Join Date
    Mar 2005
    Posts
    6

    her is the moosemunch class...

    public class MooseMunch implements Comparable {

    private String color;

    public MooseMunch (String color) {

    this.color = color;

    }

    public int compareTo(Object o) {

    return this.color.compareTo((String) o);

    }

    public String toString() {

    return this.color;

    }

    } // MooseMunch

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center