使用对象的数组或arrayList,这样文件中的所有记录都可以存储在内存中



我是Java初学者,我的练习有问题。

这是我的Client.txt文件:

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

这是我的客户端类(有构造函数(:

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

这是我的代码创建对象,并读取从txt文件中读取的记录:

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

// map each token that is already printed to corresponding field in Client class
// Because tokens[0] is of type String but clientID is of type int,
// we need to parse it and get the integer representation.
// we also do the same thing with postcode 
int clientID = Integer.parseInt(tokens[0]);
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]); 

// 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");

} // end while

练习的要求是,现在我必须修改程序以使用该客户端对象的数组或arrayList,这样文件中的所有客户端记录都可以很容易地存储在内存中,并且每个对象都是从文件中创建的,将其放入客户端对象的array/arrayList 中

我刚开始学习数组和数组列表几周,所以我不知道做这个练习。有人能帮我吗?

while循环之前创建一个ArrayList

ArrayList<Client> cList = new ArrayList<> ();

在你的循环中,

Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);
// add to list
cList.add (client);

在while循环之上,您可以简单地创建一个新的列表

List<Client> clientList = new ArrayList<>();

然后在制作每个new Client(..)后添加到客户端列表

clientList.add(client)

您可以覆盖Client的构造函数,这样它也可以只接收文件中的一行:

import java.util.stream.Stream;
public class Client {
private int clientId;
private String firstName;
private String lastName;
private String street;
private String suburb;
private String state;
private int postcode;
public Client(int clientId, String firstName, String lastName, String street, String suburb, String state,
int postcode) {
this.clientId = clientId;
this.firstName = firstName;
this.lastName = lastName;
this.street = street;
this.suburb = suburb;
this.state = state;
this.postcode = postcode;
}
public Client(String line) {
String[] split = line.split(",");
this.clientId = Integer.parseInt(split[0]);
this.firstName = split[1];
this.lastName = split[2].trim();
this.street = split[3];
this.suburb = titleCase(split[4]);
this.state = split[5];
this.postcode = Integer.parseInt(split[6]);
}
@Override
public String toString() {
return String.format("%s %s - %s, %s - %s, %d", this.firstName, this.lastName, this.street, this.suburb,
this.state, this.postcode);
}
private String titleCase(String suburb) {
return Stream.of(suburb.split(" ")).map(w -> w.toUpperCase().charAt(0) + w.toLowerCase().substring(1))
.reduce((s, s2) -> s + " " + s2).orElse("");
}
}

还可以为您的扫描仪使用试用资源:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
List<Client> clientList = new ArrayList<>();
try (Scanner scanner = new Scanner(new File("Clients.txt"))) {
while (scanner.hasNext()) {
String line = scanner.nextLine();
clientList.add(new Client(line));
}
} catch (IOException e) {
e.printStackTrace();
}
for (Client client : clientList) {
System.out.println(client);
}
}
}

输出:

Jay Walker - 91 Boland Drive, Bagotville - NSW, 2477
Mel Lowe - 45 Ocean Drive, Millers Point - NSW, 2000
Hugh Manatee - 32 Edgecliff Road, Redfern - NSW, 2016
Elizabeth Turner - 93 Webb Road, Mount Hutton - NSW, 2290

在这里试试。

最新更新