开发者

Input from stdin, separated values into an ArrayList to sort

I am trying to input a file into my program. The input is a file piped to "standard in".

Here is an example input:

3
&
Pink Frost&Phillipps, Martin&234933
Se quel guerrier io fossi&Puccini, Giacomo&297539
Non piu andrai&Mozart&234933
M'appari tutt'amor&Flotow, F&252905

My program is about sorting songs/mp3's in order of title, composer, and running time.

  1. First thing I want to do is to get the topmost integer, which in the example input is "3", and store it in an integer of my own.
  2. The next line contains the "separator" character which, as you might see, they use to separate the title, composer, and running time for each song.
  3. The next lines are an indeterminate amount of songs with title, composer, and running time details. What I would like to do with this is to input this into either an ArrayList or a LinkedList so that I can use a comparator to mergesort these using Collections.sort().

This 开发者_如何学Pythonis what I've got so far:

    public static void main(String args[]) throws IOException {
    ArrayList<Song> songs = new ArrayList<Song>();

    //Defines the stdin stream
    BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
    System.out.flush();

    int k = new Scanner(System.in).nextInt();   
    }   

So, I hope by now you know what I wish to do.

This is my query: how do I read the different values in? (i.e. the details of each song to put them into my ArrayList.)


Usually the separator and the number of fields is assumed to be known and you wouldn't put them in the file. I would do something like.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numOfFields = Integer.parseInt(br.readLine());
Pattern sep = Pattern.compile(br.readLine(), Pattern.LITERAL);
String line;
List<Song> songs = new ArrayList<Song>();
while((line = br.readLine()) != null) {
    String[] fields = sep.split(line, numOfFields);
    songs.add(new Song(fields[0], fields[1], fields[2]);
}
Collections.sort(songs);

Disclaimer: If this is homework, you have to do the way you have been shown, not just anyway which works.


Use a BufferedReader wrapping a FileReader to read the file line by line.

For each song line, use String.indexOf to find the indices of the separator char you read from the second line, and String.substring to get each part of the line as a separate string.

Use Integer.parseInt to transform the last string into an integer.

Define a Song class containing the three information about a song: title, author and time. For each song line, construct a new instance of Song and put the instance in a List<Song>.

Call Collections.sort with a comparator in order to sort your songs. Read http://download.oracle.com/javase/tutorial/collections/interfaces/order.html for information about sorting.

The documentation about all these classes and methods can be found here: http://download.oracle.com/javase/6/docs/api/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜