Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Daniel Silva
Daniel Silva
5,353 Points

Java can't find my csv file

I have the csv file in the source folder where this class is located. I get an exception thrown at me.

package books;


import java.io.BufferedReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.omg.CORBA.portable.InputStream;

public class BookApp {

    public static void main(String[] args){
        List<Book> b = Book.getList("src/books.csv");

        System.out.println();

        System.out.println("Sorted List:");
        Collections.sort(b);
        for(Book book: b){
            System.out.println(book.toString());
        }

        Comparator c = Collections.reverseOrder();
        Collections.sort(b, c);
        System.out.println();

        System.out.println("Reverse Order:");
        for(Book book: b){
            System.out.println(book.toString());
        }
    }

}



package books;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

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 +" by " + author +"("+year+")";
    }

    public static List<Book> getList(String f){
        List<Book> books = new ArrayList<>();
        int bookNo = 0;
        File file = new File(f);

        Scanner input;
        try {
            input = new Scanner(file);

            while(input.hasNext()){
                String[] parts = input.nextLine().split(",");
                if(parts.length==3){
                    Book b = new Book(parts[0],parts[1],Integer.parseInt(parts[2]));
                    books.add(b);
                    bookNo++;
                }else{
                    System.out.println("Problem reading: "+Arrays.toString(parts).replace("[","").replace("]", ""));
                }
            }
            input.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }
        System.out.println("Number of books: "+bookNo);

        return books;

    }

    @Override
    public int compareTo(Book o) {
         return this.getTitle().compareTo(o.getTitle());
    }
}

1 Answer

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

if its in the same folder, maybe try using:

        List<Book> b = Book.getList("books.csv");
Daniel Silva
Daniel Silva
5,353 Points

Hi Alex, thanks for comment. I actually tried doing that too and the compiler threw the same error.

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

what was the compiler error?

Daniel Silva
Daniel Silva
5,353 Points

the file does not exits.

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

The whole sentence? Can you just copy paste the error it throws?

Daniel Silva
Daniel Silva
5,353 Points

I'd be happy to do that. I'll have to post it later today as I'm currently at work :-/.

Daniel Silva
Daniel Silva
5,353 Points

This is the error I'm getting.

java.io.FileNotFoundException: books.csv (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.util.Scanner.<init>(Scanner.java:611) at books.Book.getList(Book.java:45) at books.BookApp.main(BookApp.java:14)