• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList & Collections

 
G. Graz
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
G. Graz
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, will do ! my Logic was half there ! Thank you !
 
G. Graz
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic