Reading to a Generic Arraylist and LinkedList from a file

Viewed 39

I am a beginner Java Programmer. I am trying to read a file called "books.csv" to my array list and linked list. I would like to know how to make the array list and linked list be able to read all types of data i.e. be generic to all data types that are stored in the books file.

My current file reading code and insertion sort code are as follows. Please recommend ways for me to make the Lists read all types of data.

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

public class Assignment_1 
{


    public static void main(String[] args)
    {
         

    }

     
    public static void readfile() 
    {
        
        try {
    
        
                Scanner s = new Scanner(new File("/Users/riybin_0211/Desktop/CS214 Assignment/books.csv"));
                ArrayList<Book> list1 = new ArrayList<Book>();
         
                while(s.hasNext())
                {
                    list1.add(s.next());
                    
                }
         
                s.close();
                
                if(list1.add(null)) {
                    System.out.println(" File was read successfully");
                }
         
         
                Scanner S = new Scanner(new File("/Users/riybin_0211/Desktop/CS214 Assignment/books.csv"));
                LinkedList<Book> List1 = new LinkedList<Book>();
         
                while(S.hasNext())
                {
                    List1.add(S.next());
                }
         
                S.close();
                System.out.println(" File was read successfully");
            }
        
        catch (IOException e)
                {
                e.printStackTrace();
                }
    }
    
    public static <T extends Comparable<T>> void insertionSort(ArrayList<T> data) {
        int i, x =0;
        T key;
        for (i=1;i<data.size();i++){
            key= data.get(i);
            while (x>=0 && data.get(x).compareTo(key) > 0){
                data.set(x+1,data.get(i));
                x--;
            }
            data.set(x+1,key);
        }
        
        
        
      }```
0 Answers
Related