Results 1 to 11 of 11
  1. #1
    Tabula Rasa is offline Member
    Join Date
    Apr 2011
    Location
    USA
    Posts
    15
    Rep Power
    0

    Default Why is my ArrayList not sorted?

    I am trying to use the compareTo method from one class, and sort the data from a file in an ArrayList, but when the program runs, the ArrayList is not sorted.
    The data in the file is:

    Siena,Catherine,4.0,AppliedMath
    Gateau,Jean,3.5,AppliedComputing
    Jones,Jill,3.0,AppliedMath
    Jones,Jack,2.5AppliedComputing

    The names must be sorted alphabetically according to last name, then first name, so that it prints this:

    Gateau,Jean,3.5,AppliedComputing
    Jones,Jack,2.5AppliedComputing
    Jones,Jill,3.0,AppliedMath
    Siena,Catherine,4.0,AppliedMath


    My compareTo method from the AppliedStudent class:

    public int compareTo(Object n)
    {
    AppliedStudent s = (AppliedStudent)n;
    int num = lastname.compareTo(s.getLast());
    return (num != 0 ? num : firstname.compareTo(s.getFirst()));
    }

    public String getLast()
    {
    return lastname;
    }

    public String getFirst()
    {
    return firstname;
    }




    my main class:


    import javax.swing.JFrame;
    import java.io.FileNotFoundException;
    import java.io.File;
    import java.io.PrintWriter;
    import java.util.*;





    public class StudentViewer
    {
    public static void main(String[] args) throws FileNotFoundException

    {


    File inputFile;
    //File outputFile;

    if (args.length >= 1)

    {
    inputFile = new File(args[0]);
    //outputFile = new File(args[1]);
    Scanner in = new Scanner(inputFile);

    ArrayList<AppliedStudent> names = new ArrayList<AppliedStudent>();

    while(in.hasNext() && in.hasNextDouble())
    {
    String line = in.next();
    Scanner lineScanner = new Scanner(line);
    lineScanner.useDelimiter("\\s*,\\s*");

    String lastname = lineScanner.next();
    String firstname = lineScanner.next();
    double gpa = lineScanner.nextDouble();
    String major = lineScanner.next();

    names.add(new AppliedStudent(lastname, firstname, gpa, major));
    }

    Collections.sort(names);

    System.out.println(names);



    }
    else
    throw new FileNotFoundException("File Not Found");

    JFrame frame = new StudentFrame(inputFile);
    frame.setTitle("Applied MNS Students");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);



    }
    }

  2. #2
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Please, as the first thing you do on these forums, wrap your code in
    "(code)"
    and
    "(/code)" (using brackets "[" and "]" instead of using parenthesis) blocks of text, so your code can be read easier. It'll look like this:
    Java Code:
    Your code here.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    1. Your code won't compile

    Java Code:
    while(in.hasNext() && in.hasNextDouble())
    2. That will never be true for your data. It is asking if there is something on the next line and is it a double. Since the first token on each line is a String it will return false.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Have a look at this sample for a neat way of sorting arraylist objects:

    Java Code:
            Person p = new Person("Bruce", "Willis");
            Person p1  = new Person("Tom", "Hanks");
            Person p2 = new Person("Nicolas","Cage");
            Person p3 = new Person("John","Travolta");
     
            ArrayList list = new ArrayList();
            list.add(p);
            list.add(p1);
            list.add(p2);
            list.add(p3);
     
            Collections.sort(list, new Comparator(){
     
                public int compare(Object o1, Object o2) {
                    Person p1 = (Person) o1;
                    Person p2 = (Person) o2;
                   return p1.getFirstName().compareToIgnoreCase(p2.getFirstName());
                }
     
            });
     
            System.out.println(list);

    Sorting an ArrayList of objects. | Some Java, JEE and WebSphere stuffs

  5. #5
    Tabula Rasa is offline Member
    Join Date
    Apr 2011
    Location
    USA
    Posts
    15
    Rep Power
    0

    Default

    Thanks. My code works just fine on Netbeans at home, but when I get to school and try to run it, it doesn't work, even on Netbeans. The school computers have the latest JDK so I have no clue what the issue is.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Explain it doesn't work.
    What errors do you get? Are you providing the files and the right paths on that school computer?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by Tabula Rasa View Post
    My code works just fine on Netbeans at home, but when I get to school and try to run it, it doesn't work, even on Netbeans.
    I find that very hard to believe.

  8. #8
    Tabula Rasa is offline Member
    Join Date
    Apr 2011
    Location
    USA
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Explain it doesn't work.
    What errors do you get? Are you providing the files and the right paths on that school computer?
    My program is supposed to read any file the user puts in and sort the data according to last name, then first name.
    The file I am testing is in the same directory as my program.

    In my AppliedStudent class, my compareTo method is:
    Java Code:
    public int compareTo(Object n)
        {
           AppliedStudent s = (AppliedStudent)n;
           int num = lastname.compareTo(s.getLast());
           return (num != 0 ? num : firstname.compareTo(s.getFirst()));
        }

    In my StudentViewer class, when I have
    Java Code:
    while(in.hasNext())
    This occurs: http://i404.photobucket.com/albums/p...z/javapic2.jpg

    When I include
    Java Code:
    while (in.hasNext() && in.hasNextDouble())
    This occurs: http://i404.photobucket.com/albums/p...z/javapic1.jpg

    ...but the data is not sorted.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you never set the Results box in your frame thats why its empty?

    EDIT:

    so it looks like you got it working with the new while loop but you need to show us the StudentFrame class so we can find out why the results aren't displaying

  10. #10
    Tabula Rasa is offline Member
    Join Date
    Apr 2011
    Location
    USA
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    you never set the Results box in your frame thats why its empty?

    EDIT:

    so it looks like you got it working with the new while loop but you need to show us the StudentFrame class so we can find out why the results aren't displaying
    I did not clarify before that right now, I'm simply trying to get the data sorted and I'm letting the data print out in the console window. The problem is, the data is not sorted.

    However, when I process the same code in Netbeans, the ArrayList is sorted.

    http://i404.photobucket.com/albums/p...z/javapic3.jpg
    Last edited by Tabula Rasa; 04-20-2011 at 06:43 PM.

  11. #11
    Tabula Rasa is offline Member
    Join Date
    Apr 2011
    Location
    USA
    Posts
    15
    Rep Power
    0

    Default

    Well I transferred all my code from Netbeans to notepad, and now the ArrayList is sorted.

    :)

Similar Threads

  1. Checking if an ArrayList is sorted
    By MonkeyGrad in forum New To Java
    Replies: 4
    Last Post: 04-13-2011, 06:27 AM
  2. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  3. Casting an int from a sorted set
    By Bmack in forum New To Java
    Replies: 2
    Last Post: 03-17-2010, 07:09 PM
  4. Sorted LinkList problem
    By koolaqua16 in forum Advanced Java
    Replies: 1
    Last Post: 08-08-2009, 06:49 AM
  5. My doublyLinked list does not get sorted
    By hasani6leap in forum New To Java
    Replies: 0
    Last Post: 01-06-2008, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •