我是Java的完全入门者,我进行了一次练习,我必须从CSV文件中读取数据,然后在程序从文件中读取数据时为文件的每一行创建一个对象。 / p>
这是CSV文件的一部分:
1,Jay, Walker,91 Boland Drive,BAGOTVILLE,NSW,2477
2,Mel, Lowe,45 Ocean Drive,MILLERS POINT,NSW,2000
3,Hugh, Manatee,32 Edgecliff Road,REDFERN,NSW,2016
4,Elizabeth, Turner,93 Webb Road,MOUNT HUTTON,NSW,2290
以此类推...
这是我的代码,可从CSV文件读取数据:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Client_19918424 {
public static void main(String[] args) throws FileNotFoundException {
File inFile = new File("clients.txt");
Scanner inputFile = new Scanner(inFile);
String str;
String[] tokens;
while (inputFile.hasNext()) {
str = inputFile.nextLine(); // read a line of text from the file
tokens = str.split(","); // split the line using commas as delimiter
System.out.println("Client ID: " + tokens[0]);
System.out.println("Client First Name: " + tokens[1]);
System.out.println("Client Sur Name: " + tokens[2]);
System.out.println("Street Address: " + tokens[3]);
System.out.println("Suburb: " + tokens[4]);
System.out.println("State: " + tokens[5]);
System.out.println("Postcode:" + tokens[6]);
System.out.println( );
} // end while
}
}
这是我的Client类(具有构造函数):
public class Client {
private int clientID;
private String firstName;
private String surName;
private String street;
private String suburb;
private String state;
private int postcode;
// constructor
public Client (int ID, String fName, String sName, String str, String sb, String sta, int pCode) {
clientID = ID;
firstName = fName;
surName = sName;
street = str;
suburb = sb;
state = sta;
postcode = pCode;
}
但是,当程序从文件中读取数据时,我不知道如何为文本文件的每一行创建一个Client
对象。
像第一行这样:
Client client1 = new Client(1, "Jay", "Walker", "91 Boland Drive", "BAGOTVILLE", "NSW", 2477);
然后将其添加到数组:
Client[0] = client1;
有人可以帮我解决这个问题,我真的很感激。
答案 0 :(得分:0)
您快到了。
剩下要做的就是将每个已打印的令牌映射到Client
类中的相应字段。由于token[0]
并没有真正说出它拥有什么价值,您可以通过三种方式做到这一点:
while (inputFile.hasNext()) {
str = inputFile.nextLine();
tokens = str.split(",");
// Because tokens[0] is of type String but clientID is of type int,
// we need to parse it and get the integer representation.
int clientID = Integer.parseInt(tokens[0]);
// Both of type String, no parsing required.
String firstName = tokens[1];
String surName = tokens[2];
String street = tokens[3];
String suburb = tokens[4];
String state = tokens[5];
int postcode = Integer.parseInt(tokens[6]);
// Then all that's left to do is to create a new object of `Client` type
// and pass all the gathered information.
Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);
System.out.println(client + "\n");
}
这时,如果我们尝试打印客户端(最后一行),我们将得到如下内容:com.example.demo.Client@30a3107a
。那是因为我们没有告诉我们如何显示对象。对于toString()
类中的Client
方法,必须这样重写:
@Override
public String toString() {
return "Client ID: " + clientID + "\n" + "Client First Name: " + firstName + "\n"
+ "Client Sur Name: " + surName + "\n" + "Street Address: " + street + "\n"
+ "Suburb: " + suburb + "\n" + "State: " + state + "\n" + "Postcode: " + postcode;
}
它将给出示例中的确切输出。
也可以通过直接传递这些标记来创建类,而无需创建临时变量:
Client client = new Client(Integer.parseInt(tokens[0]), tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], Integer.parseInt(tokens[6]));
这种情况将我们带到使用setter和getter的第三个解决方案。
描述Client
的变量已经定义,可以传递它们来组装完美的对象,但是无法检索它们。代替直接在构造函数中设置变量,我们可以创建一个可以完成此任务的特殊方法,例如:
// Other fields omitted
private int clientID;
// The empty constructor required for later usage,
// since right now, we can't create the object without specifying every property.
public Client() {
}
// This method does exactly the same thing that was done before but
// in the constructor directly
public void setClientID(int clientID) {
this.clientID = clientID;
}
// This method will assist in retrieving the set data from above.
public int getClientID() {
return clientID;
}
然后while loop
看起来像这样:
Client client = new Client();
client.setClientID(Integer.parseInt(tokens[0]));
client.setFirstName(tokens[1]);
client.setSurName(tokens[2]);
client.setStreet(tokens[3]);
client.setSuburb(tokens[4]);
client.setState(tokens[5]);
client.setPostcode(Integer.parseInt(tokens[6]));
并获得这些值:
System.out.println("Client ID: " + client.getClientID());
或者,如果只能在所有字段都存在的情况下创建客户端,则可以将构造函数与字段一起使用来创建客户端,在类中添加getter,省略两个setter,以及空的构造函数。 >