Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please use the code button and maintain indentation in quoted code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic