File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ArrayList & Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList & Collections" Watch "ArrayList & Collections" New topic
Author

ArrayList & Collections

G. Graz
Ranch Hand

Joined: Oct 23, 2006
Posts: 30
I need to bounce an idea off someone to see if I am heading down the right direction with how I want to write a program. I need to read a .txt file , the file will have lines of data with each line containing the following :
student grade, student name , student ID . The .txt file has no set length so I am gong to use an ArrayList to store the data , problem that I am having is when I recover the data it's all in one array.

ex. [ 100, bob, 55555, 96, tim, 12345, 87, amy, 54321 ]

I need to break the ArrayList down so I can use the Collections.sort to sort the array date by name . so the result would look like:

amy 87 54321
bob 100 55555
tim 96 12345

This is where I need the help ...I was thinking about using a for loop to sort the data out , then apply the Collections.sort to it after my loop is complete. Would using a "For Loop" be going in the right directions with this ? Thank you for the help !
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Define a class called Student with name, id, and grade fields. As you parse each line of the text file, create a Student, put it in the ArrayList.

Then to sort by name, grade, or id, you write a tiny class that implements java.util.Comparator<Student> by comparing one of the three fields. Pass an instance of your Comparator to the sort() method, et voila!


[Jess in Action][AskingGoodQuestions]
G. Graz
Ranch Hand

Joined: Oct 23, 2006
Posts: 30
Ok, will do ! my Logic was half there ! Thank you !
G. Graz
Ranch Hand

Joined: Oct 23, 2006
Posts: 30
From the .txt file I created two Arrays , “Text” & “Int” array. So, I am now in my Comparator class , I want to bind each index of the arrays that I created with something like :

Student[0] = new student();
Student{Text.size[i]].setScore ( Text[i]);
Student[Int.size[i]].setName(Int[i]);

I know I need to create some type of loop that gets and sets what I want to use the Comparator on, and the above example is probably way off but my question is , “Can this be done “ ? My .txt file has no set amount of data so I have to use some type of generic so my ArrayList is populated with what is given in the .txt file . I will post my code if I am not getting my question across correctly. Thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37977
    
  22
You have misunderstood what Ernest suggested.

As you read the text file, create a Student object with the name and ID and grade.
As long as your text file always runs in order ID-name-grades ID-name-grades, you can use the nextXXX methods of the Scanner class to find ID name grade and pass them to the constructor of a Student object.
If 12345 means grade1 for module 1, grade 2 for module 2, grade 3 for module3, etc, you can tease that number apart with the / 10 and % 10 operations. You can even count how many modules there are with a method from the Math class (maybe log10): log10(55555) = 4.7447.... so you can easily get 5 by adding 1 and casting to an int.

Now you have your List<Student> you can sort it by passing a Comparator which compares the names; if name is a String, then String already implements the Comparable<String> interface.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Once you finish with solving the problem, you could think about changing your collection to TreeSet.........(just think whether you really need it, don't change)
A classic problem well explained in Head First Java.


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList & Collections
 
Similar Threads
Out of Sorts
Need help on which collection (key and value, with value sorted)
Help with an array situation
Exception in thread "main" java.lang.Stack
Printing out of an data stored in and array and SringBuffer array