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

    Default Read text file and sort in ArrayList

    I want to have the Scanner read the text file, and sort the data in an ArrayList.

    My fileReader and ArrayList are in my main class, but I don't know how to transfer that data to the ArrayList.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

    Default

    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]);


    AppliedStudent nameArray[] = inputFile; (This is wrong but I don't know how to correct it.)

    List<AppliedStudent> names = Arrays.asList(nameArray);
    Collections.sort(names);
    System.out.println(names);


    JFrame frame = new StudentFrame(inputFile);
    frame.setTitle("Students");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }
    else
    throw new FileNotFoundException("File Not Found");
    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

    Default

    Scanner in = new Scanner(new File(args[0]));
    for(int i=0; in.hasNext(); i++)
    while (in.hasNext())
    {
    ?
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't want both a for loop and a while loop. A while loop alone is probably best, and you'll likely want to check hasNextLine and then in the loop, get nextLine() depending on the text file's format.

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

    Default

    It reads and prints out the data.

    I have to alphabetize the data, so how do I apply the compareTo method I did in a different class?

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

    I tried to use Collections.sort but it didn't apply the method.

    The names are supposed to be sorted by last name, then first name.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Tabula Rasa View Post
    It reads and prints out the data.

    I have to alphabetize the data, so how do I apply the compareTo method I did in a different class?
    compareTo is used in a class that implements the Comparable interface. So if AppliedStudent or its parent implements this interface, then you're all set. If not, then you can't use Comparator or its compareTo method, but will have to create a Comparator<AppliedStudent> utility class and this class will contain one static method called compare that will take two AppliedStudent objects as parameters).

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2010, 06:31 AM
  2. Read txt file into arraylist
    By nickerhardt in forum New To Java
    Replies: 9
    Last Post: 08-04-2010, 05:34 PM
  3. Read txt file to arrayList
    By koddy in forum New To Java
    Replies: 14
    Last Post: 04-29-2010, 06:15 PM
  4. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:03 AM
  5. Replies: 2
    Last Post: 01-26-2009, 05:28 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
  •