Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What is wrong with the call?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is wrong with the call?

    This compiles and runs well:
    public class IntegerTest implements Comparator<Integer>{
    @Override
    public int compare(Integer o1, Integer o2){
    return (o1<o2 ? -1 : (o1==o2 ? 0 : 1));
    }
     
     
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    List<Integer> arrayList = new ArrayList<>();
    arrayList.add(12);
    arrayList.add(5);
    arrayList.add(7);
    arrayList.add(4);
    arrayList.add(5);
    arrayList.add(16);
    arrayList.add(13);
    arrayList.add(2);
    System.out.println(arrayList);
    Collections.sort (arrayList, new IntegerTest());
    for (Integer integer : arrayList){
    System.out.println (integer);
     
    }
    }
    }
    This code creates and prints the array but gives me an error at Collections.sort.
    What is wrong with the call?
    public class ListSorter implements Comparator<Integer>{
     
    @Override
    public int compare(Integer o1, Integer o2){
    return (o1>o2 ? -1 : (o1==o2 ? 0 : 1));
    }
     
    }
    public static void main(String[] args)throws IOException {
    java.io.File intDat = new java.io.File("intDat");
    try (java.io.PrintWriter create = new java.io.PrintWriter (intDat)) {
    create.print("23 45 67 12 9 44 14 21 78 95 6 89 7 42 28 34");
    }
    Collection<Integer> intList;
    try (Scanner read = new Scanner (intDat)) {
    intList = new ArrayList<>();
    //List<Integer> arrayList = new ArrayList<>();
    while (read.hasNext()){
    intList.add(read .nextInt());
     
     
    System.out.println(intList);
    }
    }
    //System.out.println(arrayList);
     
    Collections.sort (intList, new ListSorter());
    for (Integer integer : intList){
    System.out.println (integer);
    }
     
    }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: What is wrong with the call?

    When you are asking about an error or exception, it makes sense to always post the error or exception message here for our review.

    --- Update ---

    Why are you asking this question twice? Please go back to your original post. Please show the error message as has been asked there.

    Locking this as an unnecessary duplicate

Similar Threads

  1. call by value and call by reference
    By Appu14 in forum The Cafe
    Replies: 1
    Last Post: September 2nd, 2012, 06:58 AM
  2. How to call table?
    By matsnys in forum Member Introductions
    Replies: 1
    Last Post: August 15th, 2012, 01:49 PM
  3. How to call database???
    By mahoppe in forum JDBC & Databases
    Replies: 2
    Last Post: August 23rd, 2011, 08:31 PM
  4. Call by Reference & Call by Value
    By Lokesh in forum Java Theory & Questions
    Replies: 1
    Last Post: February 21st, 2011, 01:19 PM
  5. Call with a different signature
    By brajesh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 15th, 2010, 02:05 PM