Click here to Skip to main content
15,569,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
package assignment1;

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

/**
 *
 * @author User
 */
class book{
               private static int bookID;
               private static String title;
               private static String author;
               private static float average_rating;
               private static String isbn;
               private static String isbn13;
               private static String language_code;
               private static long num_pages;
               private static long ratings_count;
               private static long text_reviews_count;
               private static String publication_date;
               private static  String publisher;
      
    //Getter and Setter methods
    public void setbookID(int bookID) {
            book.bookID = bookID;
    }           
    public int getbookID() {
            return bookID;
    }
    public void settitle(String title) {
            book.title = title;
    }

    public String gettitle() {
            return title;
    }

    public void setauthor(String author) {
            book.author = author;
    }
    
    public String getauthor() {
            return author;
    }

     public void setaverag_rating(float average_rating) {
            book.average_rating = average_rating;
    }
     public float getaverage_rating() {
            return average_rating;
    }

   public void setisbn(String isbn) {
            book.isbn = isbn;
    }
    
    public String getisbn() {
            return isbn;
    }

    public void setisbn13(String isbn13) {
            book.isbn13 = isbn13;
    }
    
    public String getisbn13() {
            return isbn13;
    }
    public void setlanguage_code(String language_code) {
            book.language_code = language_code;
    }
    public String getlanguage_code() {
            return language_code;
    }

     public void setnum_pages(long num_pages) {
            book.num_pages = num_pages;
    }
    
        public long getnum_page() {
            return num_pages;
    }
    public void setratings_count(long ratings_count) {
            book.ratings_count = ratings_count;
    }
    public Long getratings_count() {
            return ratings_count;
    }

     public void settext_reviews_count(long text_reviews_count) {
            book.text_reviews_count = text_reviews_count;
    }
       public Long gettext_reviews_count() {
            return text_reviews_count;
    }

    public void setpublication_date(String publication_date) {
            book.publication_date = publication_date;
    }
    public     String getpublication_date() {
            return publication_date;
    }

   public void setpublisher(String publisher) {
            book.publisher = publisher;
    }
    public String getpublisher() {
            return publisher;
    }
 public void printDetails() {
        System.out.println(bookID +","+title+","+author+","+average_rating+","+isbn+","+isbn13+","+num_pages+","+language_code+","+ratings_count+","+text_reviews_count+","+publication_date+","+publisher);
    }
  
}//end of book class

public class Main
{
    
    public static void main(String[] args) {
       
        List<book> ArrayList;
        List<book> LinkedList;
        ArrayList = new ArrayList<>();
        LinkedList = new LinkedList<>();
        String[] bookCsv;
        
        int counter = 0;
        //read file func
        File filepath=new 
//creates a new file instance File("C:\\Users\\User\\Documents\\NetBeansProjects\\books.csv");
         try (BufferedReader br = new BufferedReader(new FileReader(filepath)))
        {  
            String line;  
            int iteration= 0;
            while((line=br.readLine())!=null)  
            {  
                //skip first line
                if (iteration==0){
                    iteration++;
                    continue;
                }
                
                 bookCsv = line.split(",");
                book bookobj = new book();//new book
                
              
                bookobj.setbookID(Integer.parseInt(bookCsv[0]));
                bookobj.settitle(bookCsv[1]);
                bookobj.setauthor(bookCsv[2]);
                bookobj.setaverag_rating(Float.parseFloat(bookCsv[3]));
                bookobj.setisbn((bookCsv[4]));
                bookobj.setisbn13((bookCsv[5]));
                bookobj.setlanguage_code(bookCsv[6]);
                bookobj.setnum_pages(Long.parseLong (bookCsv[7]));
                bookobj.setratings_count(Long.parseLong(bookCsv[8]));
                bookobj.settext_reviews_count(Long.parseLong(bookCsv[9]));
                bookobj.setpublication_date((bookCsv[10]));
                bookobj.setpublisher(bookCsv[11]);
                
                ArrayList.add(bookobj);
                LinkedList.add(bookobj);
                counter++;
                
            }    
        }  
            catch(IOException e)  
            {  
                System.out.println("No file found");
            }  

         Main Data = new Main();
         
         //print Arraylist
         for (int i = 0; i <= ArrayList.size();i++)
         {
            System.out.println(Data.getbookID()+" "+ Data.gettitle()+" "+Data.getauthor()+" "+Data.getratings_count()+" "+Data.getisbn()+" "+Data.getisbn13()+" "+Data.getlanguage_code()+" "+Data.getnum_page()+" "+ Data.gettext_reviews_count()+" "+ Data.getpublication_date()+" "+Data.getpublisher());

         }
          System.out.println("Number of Contents of File: "+counter+" " + "while loop");
    } 
    
}


What I have tried:

i tried reading a csv file into a arraylist and storing it into a object but when printing, it always print the same array index, how can i change that?
Posted
Updated 24-Sep-22 19:01pm
v3

1 solution

First off I would use the green Improve Question link to enclose your code in a code block. Select the code text and click the code tool. Then fix the bracing and indenting. It'll make the code far easier to read.

Here are my thoughts after looking over your code:

Your Main class doesn't need to extend book. There's no need for the Data instance either. Main only really exists as an entity to run the program via its included main method.

Your code reads the CSV file into an ArrayList. When you want to print it out loop thru ArrayList and print out each ArrayList entry. ArrayList.get(i) will return each book instance that was added to the list.

for (int i = 0; i < ArrayList.size();i++)
{
    bookobj = ArrayList.get(i);
    Bookobj.printDetails();
}

System.out.println("Number of Contents of File: "+counter+" " + "while loop");


Why are there 2 lists, ArrayList and LinkedList? If this is related to your earlier insertion sort question. I would suggest changing your ArrayList and LinkedList variables.

Change List<book> LinkedList to LinkedList<book> unsortedList;
Change List<book> ArrayList to ArrayList<book> sortedList;
Read the CSV file into unsortedList.
Then loop through unsortedList doing an insertion sort into sortedList.
Finally loop thru sortedList to print it out.
 
Share this answer
 
Comments
John Bob 2021 24-Sep-22 23:09pm    
well the assignment question says, Read this data in your Java program by creating a Book class and storing it into a linked list and array list separately. Now do the following:
1. Using polymorphism technique with list data structure as the super class wherever applicable, create the following sorting algorithms:
a. Insertion Sort with a linked list and array list.
b. Exchange sort or Bubble sort with a linked list and array list
FreedMalloc 24-Sep-22 23:59pm    
It sounds to me that you should create your own list classes that extend the Java LinkedList and ArrayList classes to add the sort methods. That way you can read the CSV file into the your new list classes and call their sort methods to sort the lists. However, I don't have access to your assignment so you should confirm and clarify this with your instructor.
John Bob 2021 25-Sep-22 1:11am    
yes, it should be done like that, but im a little bit confused on how to go about doing it, can you draw a short flow chat or pseudo code as example?
FreedMalloc 25-Sep-22 15:33pm    
Your main method is pretty close now, it parses the CSV file and creates book objects. What's left is to create the classes that extend the LinkedList and the ArrayList and add the sort methods to them according to your assignment. Add each created book to your new list class instances instead of the lists you have now. Finally call the sort method and print out the results.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900