This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList" Watch "ArrayList" New topic
Author

ArrayList

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
I have a file named Songs.txt. Now i want to read the data in this file and store it in an ArrayList named SongList and sort it,using sort().
I have written the following program,but I am not able to proceed further.Please Help.


import java.io.*;
public class Song
{
public static void main(String[] args)
{
try
{
File file=new File("Songs.txt");
FileReader r=new FileReader(file);
BufferedReader b=new BufferedReader(r);
String line=null;
while((line=b.readLine())!=null)
{
System.out.println(line);
}
}
catch(IOException e)
{
}
}
}
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

If you are getting a string 'line' , which contains the song name, why don't you make an instance of ArrayList and add 'line' into it !!

And the call sort() method to sort them !!

It's simple, isn't it ?


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
If you haven't used any of the Collections classes before, you may want to work through the Java Tutorial chapter on that: http://java.sun.com/docs/books/tutorial/collections/index.html


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
And please use the code button and maintain indentation in quoted code.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ArrayList