0
package books;

 import java.awt.List;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.ArrayList;

 public class Book implements Comparable<Book>{

private String title;
private String author;
private int year;

public Book(String title, String author, int year){
    this.title = title;
    this.author = author;
    this.year = year; 
}

public String getTitle() {
    return title; 
}
public String getAuthor() {
    return author;
}
public int getYear() {
    return year;
}
 @Override
public String toString() {
    return title + author + "("+ year + ")"; 
}
public static void getList(String file) throws IOException {
    try {
        ArrayList<Book> books= new ArrayList<Book>();
        FileReader fr = new FileReader (file);
        BufferedReader br = new BufferedReader(fr);
        String line;

        while((line=br.readLine())!=null) {
            String [] entries =line.split(",");
            //int [] entrie=line.split(",");
            Book book = new Book( title[0], author [1], year[2]);

            System.out.println(line);
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static void main( String [] args ) throws IOException {
    getList("books (2).csv");

}

I couldn't find the answer so maybe you guys could help me out but I am trying to separate the list of book titles, authors, and the year and then arrange them alphabetically. Im trying to get them to an array but I don't think that I am using the constructor correctly or something. Do you have any ideas?

  • 2
    You'll be better off using a library for CSV. It's a widely used format, so there's no need to re-invent the wheel. You're gonna have a hard time with quotes, escaped chars, charsets, header line etc. – Hubert Grzeskowiak Aug 31 '18 at 03:24
  • @HubertGrzeskowiak is exactly correct. For example, it’s totally valid to have a cell in a CSV file that contains a comma. Use a library or you’ll be sorry. – Nate Aug 31 '18 at 03:25
  • These are the two ways I can think of https://ostermiller.org/utils/CSV.html and http://opencsv.sourceforge.net/ – MithunS Aug 31 '18 at 03:30

3 Answers3

0

You need construct the Book with String[] you get:

String[] entries =line.split(",");
Book book = new Book(entries[0], entries[1], Integer.parseInt(entries[2].trim()));
xingbin
  • 23,890
  • 7
  • 43
  • 79
  • Its never a good idea to use split by comma, since your text itself in a cell can then have commas, or it could have any other delimiter. – MithunS Aug 31 '18 at 03:31
  • @MithunS But did the OP actually show data which would support your concern? I agree with you, in general, but we would need to see data to confirm this. – Tim Biegeleisen Aug 31 '18 at 03:33
  • I can go on, but the same reasons as given here. https://stackoverflow.com/a/24950812/2700497 – MithunS Aug 31 '18 at 03:38
  • @MithunS Yeah, it's not a good idea to read csv with low level jdk api, it's not a good idea to ignore the `FileNotFoundException`, it's not a good idea to put a space `String []` before `[]`. But, please notice the OP is just a beginner, please focus on the problem he is facing. – xingbin Aug 31 '18 at 03:39
0

You need to use the elements from the entries[] array to construct each Book object:

while ((line = br.readLine()) != null) {
    String [] entries = line.split(",");
    Book book = new Book(entries[0], entries[1], Integer.parseInt(entries[2]));
    books.add(book);
}

Then, assuming you are using Java 8, you may directly sort the list by title:

books.sort(Comparator.comparing(Book::getTitle,
    Comparator.nullsFirst(Comparator.naturalOrder())));
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
0

On your code, you send 3 split parts of a String to the constructor, but year is expected to be int. Change it to String as well and it will work.

You inherit the Comparable interface but are missing the compare function, you have to define it.

Your class isnt closed, put a } at the end.

If your books start reading, but reading the data wrong, it is because of:

    while((line=br.readLine())!=null) {
        String [] entries =line.split(",");
        //int [] entrie=line.split(",");
        Book book = new Book( title[0], author [1], year[2]);

        System.out.println(line);
    }

You are using a naive understanding of csv. Here's the format: https://tools.ietf.org/html/rfc4180#section-2

And a tutorial on parsing it: https://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/

If your csv file is curated by you to match the format you parse, then it wont be an issue.

gia
  • 757
  • 4
  • 19
  • `and then arrange them alphabetically` ... what about sorting the list of books? – Tim Biegeleisen Aug 31 '18 at 03:51
  • The comparable interface takes care of that easily, but he has to implement its function as I mentioned. After that he just calls sort on the books collection. His code will not compile until he implements the interface anyway. – gia Aug 31 '18 at 03:54