Home > Back-end >  is different data types like string int and array in the same csv file can be readable in java?
is different data types like string int and array in the same csv file can be readable in java?

Time:04-11

enter image description here

this is the model to follow for the assignment. I have created java classes as you can see down below.

class BookerPrize{
        
        private int year;
        book winner;
        List<book> shortlist;
        List<String> panel;
        String chairPerson;
        public BookerPrize( int year,book winner, List<book> shortlist, List<String> panel, String chairPerson) {
                super();
                this.winner = winner;
                this.shortlist = shortlist;
                this.panel = panel;
                this.year = year;
                this.chairPerson = chairPerson;
        }
        public book getwinner() {
                return winner;
        }
        public List<book> getshortlist() {
                return shortlist;
        }
        public List<String> getpanel() {
                return panel;
        }
        public int getYear() {
                return year;
        }
        public String getchairPerson() {
                return chairPerson;
        }
        @Override
        public String toString() {
           
                return "Winner : "   " [ "   winner.getTitle()   "  "   winner.getTitle()   "  "   winner.getTitle()   " ] "   "\n"   "year : "   year   "\n"   "chair man : "   chairPerson   "\n";
        }
        
}

and here is the book.java class

public class book {
     private String title,author,publisher;
       
        public book(String title, String author, String publisher) {
               
                this.title = title;
                this.author = author;
                this.publisher = publisher;
             
        }
        public String getTitle() {
                return title;
        }
        public String getauthor() {
                return author;
        }
        public String getpublisher() {
                return publisher;
        }
      
}

I am a little confused about how can I make get data from CSV to this pattern. I have created a dummy dataset that I believe could be suitable for this task. here you can see

int year, book [title.......,author..,publishers], List<book> [[title.......,author..,publishers],[title.......,author..,publishers],.....], List<String> [panelmember1,panelmember2,panelmember3,.....panelmember(n)], String chairperson ///////////////////////////////multiple entries as above//////////////////////////////////

Tell me if I am doing right ? ... I have tried to scan this .txt file through the scanner but this wasn't helpful because I was trying to split the line through "," but can be able to get book class parameters. let me know how can I get these parameters in java.

Here is the complete question of the problem u can check

You are required to write a Java 8 program that opens and reads a delimited data file that is located relative to the NetBeans project root folder. The delimited data file contains information about Booker prize winning and shortlisted novels. The data file is called booker-data.txt. The data file must not be altered and should be considered as a read-only data file.

The data file is delimited in a consistent structure and contains entries relating to 20 years of Booker prize winners and shortlisted nominees. Each entry contains a series of data fields representing the following information: the year of the prize, the winning author, the winning book title, the authors and book titles of shortlisted nominees, the panel members who were responsible for deciding the winner, and an indication of which panel member acted as chairperson. You are required to implement Java classes to represent the Booker prize winning information with respect to this data set. The program should parse the data file and create and store a collection of objects for each entity.

Figure 3 provides a partial UML class representation of the classes that you will need to implement. It illustrates a core class to represent the Booker prize information alongside a utility class to represent the basic details of a book. The class model indicates the data members and accessor (i.e., getter) methods that map to those data members, and a toString() method for the BookerPrize class. It is left to you to determine how the objects should be initialised. Once all the objects are loaded into the collection, the program should present the User with a console-based menu to interact with the data set.

This menu should loop until the User enters a character to exit the menu (e.g., zero as illustrated below). In addition to an exit option, the menu should offer three other options: list, select and search. On starting the program, the following menu should be displayed to the console:

---------------------- Booker prize menu
---------------------- List ................1 Select ..............2 Search ..............3 Exit.................0
---------------------- Enter choice:>

The User can simply exit the program by entering zero. The three other menu options allow the User to inspect the information in the data set (note again that this program is entirely read-only and there is no requirement to add, update or delete any part of the data set). The necessary interaction of the program with respect to these options is illustrated in Appendix A.

Note that console output should be neatly formatted, and some consideration will be given to formatting when the program is assessed. In particular, when the option to view the details of the Booker prize winner and shortlist for a given year is selected (i.e., the ‘select’ menu option), it must result in the invocation of the toString() method for that particular BookerPrize object. You are required to utilise a StringBuilder object when implementing the toString() method for the BookerPrize class.

CodePudding user response:

It sounds like you're looking for an algorithm to (1) parse CSV files and (2) convert text to a Java objects.

For (1) you can use a CSV parsing library like opencsv, check out this guide: https://mkyong.com/java/how-to-read-and-parse-csv-file-in-java/ . For (2) you can use a deserialization library like jackson, check out this guide: https://www.baeldung.com/jackson-deserialization.

When you use a java.io.Scanner to read file contents the library is reading each line and converting it into a String object (string of text). So, if you want to solve (2) yourself, you will need to first define a format on how you want to write each structure (e.g., arrays) and then create an algorithm to convert it into a Java structure (e.g., java.util.ArrayList). For example, let's say you define the format for arrays to be delimited by | (e.g., a|b|c), then you can write a function to convert this to a list and use this function as part of your program.

List toList(String csvColumnStr) {
   return Arrays.asList(csvColumnStr.split('|'))
}

CodePudding user response:

CSV is intended for flat tabular data.

You want more.

In a quoted field you could have again comma separated text, which you could parse in a second step. A List<string> for instance.

For a List<Book> (please capitalize class names) there exists an other technique: using different record types, placing in an additional first field the record type:

BookerPrize ... ... ...
Book ... ...
Book ... ...
BookerPrize ... ... ...
Book ... ...
Book ... ...

However all this is very circumstantial.

Some probably would advise to use the hierarchical JSON format.

I am more the friend of XML, which has a validated syntax, so dangerous typos are easily found, and you can use java with Jakarta XML Binding (formerly JAXB, Java Architecture for XML Binding) annotations to read and write Java objects. Quite easy. Also the types do not need hand-made conversion.

  • Related