同一csv文件中的不同数据类型(例如string int和array)可以在java中读取吗?

发布于 2025-01-20 10:09:04 字数 3823 浏览 0 评论 0原文

这是要遵循分配的模型。 我创建了Java类,如下所示。

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";
        }
        
}

这是book.java类,

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;
        }
      
}

我对如何将数据从CSV获取到此模式有些困惑。 我创建了一个虚拟数据集,我认为可以适合此任务。在这里,您可以看到

int年,书籍[标题.......,作者..,出版商],列表&lt; book&gt; [[标题....... [PANELMEMBER1,PANELMEMBER2,PANELMEMBER3,..... PANELMEMBER(n)],String Chairsperson ///////////////////////////////multiple entries as above/////////////// ////////////////////

告诉我我做对吗? ...我试图通过扫描仪扫描此.TXT文件,但这并不有用,因为我试图通过“”划分“”,但是能够获得书类参数。让我知道如何在Java中获取这些参数。

这是您可以检查的问题的完整问题

您需要编写一个Java 8程序,该程序打开并读取 相对于Netbeans项目的界定数据文件 根文件夹。划界数据文件包含有关Booker的信息 获奖和入围小说。数据文件被调用 Booker-data.txt。数据文件不得更改,应该是 被视为仅读取数据文件。

数据文件以一致的结构界定并包含 与20年Booker奖获得者有关的条目和入围的条目 提名人。每个条目包含一系列代表的数据字段 以下信息:奖金年,获奖作者, 赢得书名,候选名单的作者和书名 提名人,负责决定的小组成员 获胜者,以及小组成员担任主席的迹象。 您需要实现Java类以代表Booker 关于此数据集的获奖信息。该程序 应该解析数据文件并创建和存储一个集合 每个实体的对象。

图3提供了类的部分UML类表示 您需要实施。它说明了一个核心类 代表布克奖的信息以及公用事业课程 代表一本书的基本细节。类模型指示 数据成员和访问者(即getter)方法,将其映射到这些方法 数据成员和BookerPrize类的ToString()方法。这是 留给您以确定应如何初始化对象。一次 所有对象都加载到集合中,程序应 向用户提供基于控制台的菜单,以与数据进行交互 设置。

此菜单应循环,直到用户输入角色以退出 菜单(例如,如下图所示)。除了退出选项, 菜单应提供其他三个选项:列表,选择和搜索。在 启动程序,以下菜单应显示为 控制台:

  ----------------------------------------------------------------------------------------------------------------------
---------------------------列表............. 1选择......... ..... 2搜索.................................................................... 0
---------------------------输入选择:&gt;
 

用户可以通过输入零来退出程序。另外三个 菜单选项允许用户检查数据集中的信息 (再次请注意,此程序完全是只读的,没有 要求添加,更新或删除数据集的任何部分)。这 程序相对于这些选项的必要互动是 在附录A。

中说明

请注意,控制台输出应整洁格式,有些 考虑该程序时将考虑格式化 评估。特别是,当选项查看详细信息时 选择给定年的Booker奖获得者和入围名单(即 “选择”菜单选项),必须导致调用 该特定BookerPrize对象的ToString()方法。你是 实现时使用StringBuilder对象所需的 BookerPrize类的ToString()方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的故事 2025-01-27 10:09:05

听起来您正在寻找一种算法来 (1) 解析 CSV 文件和 (2) 将文本转换为 Java 对象。

对于 (1),您可以使用 opencsv 等 CSV 解析库,请查看本指南:https://mkyong.com/java/how-to-read-and-parse-csv-file-in-java/ 。对于 (2),您可以使用 jackson 等反序列化库,请查看本指南:https://www. baeldung.com/jackson-deserialization

当您使用 java.io.Scanner 读取文件内容时,库会读取每一行并将其转换为 String 对象(文本字符串)。因此,如果您想自己解决 (2),您需要首先定义一个关于如何编写每个结构(例如,数组)的格式,然后创建一个算法将其转换为 Java 结构(例如,java.lang.)。 util.ArrayList)。例如,假设您定义了由 | 分隔的数组格式。 (例如,a|b|c),那么您可以编写一个函数将其转换为列表并将该函数用作程序的一部分。

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

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('|'))
}
挽清梦 2025-01-27 10:09:05

CSV适用于平坦的表格数据

你想要更多。

在引用的字段中,您可以再次拥有逗号分隔的文本,您可以在第二步中解析。 list&lt; string&gt;例如。

对于list&lt; book&gt;(请大写班级名称),存在其他技术:
使用不同的记录类型,将记录类型放置在附加的第一字段中:

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

但是所有这些都非常间接。

有些人可能建议使用层次结构 json 格式。

我更像是 xml ,具有验证的语法,很容易找到危险的错别字,您可以将java与 jakarta jakarta xml em> jaxb java架构用于XML绑定)读取和编写Java对象的注释。很容易。同样,类型不需要手工制作的转换。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文