我正在尝试转换一个在每个垃圾箱中包含三条客户信息的数组:
String[] csv = {" jimmy ,johnson,jjohnson@gmail.com",
"Joe,Donald,Joe_Donald@donald.org",
"ARTHUR,THOMPSON,ARTHUR@thompson.org"};
我有一个(客户)类,该类包括一个构造函数,该构造函数使用名字,姓氏和电子邮件来创建客户。
String customerList = "";
for (int i = 0; i < csv.length; i++) {
customerList += csv[i];
}
String[] customers = customerList.split(",");
Customer[] customs = new Customer[(customers.length / 3)];
for (int i = 0; i < customers.length / 3; i += 3) {
customs[i] = new Customer(customers[i], customers[i + 1], customers[i + 2]);
}
System.out.println(customs[0].getFirst_name());
System.out.println(customs[0].getLast_name());
System.out.println(customs[0].getEmail());
这几乎使我到达了所需的位置,但是有一个小问题-当信息存储在数组中时,它不会将原始数组中的逗号视为我尝试的逗号之一用作拆分。这是上面的代码给我的:
Email Creator
=========================
jimmy
johnson
jjohnson@gmail.comJoe
如您所见,信息的前几位是正确的,但是Joe(第二个人的名字)与第一位顾客在一起。
答案 0 :(得分:2)
通话
customerList += csv[i];
将为您提供一个看起来像
的字符串 jimmy ,johnson,jjohnson@gmail.comJoe,Donald,Joe_Donald@donald.orgARTHUR,THOMPSON,ARTHUR@thompson.org
可能有多种方法可以修复它,但是在将csv数组中的每个条目连接起来后,我会尝试添加逗号:
customerList += csv[i] + ",";
答案 1 :(得分:2)
您为什么需要String customerList = "";
?
您可以这样获取海关数组:
String[] csv = {" jimmy ,johnson,jjohnson@gmail.com",
"Joe,Donald,Joe_Donald@donald.org",
"ARTHUR,THOMPSON,ARTHUR@thompson.org"};
Customer[] customs = new Customer[csv.length];
for (int i = 0; i < csv.length; i++) {
String[] splitted = csv[i].split(",");
customs[i] = new Customer(splitted[0].trim(), splitted[1].trim(), splitted[2].trim());
}
答案 2 :(得分:1)
使用流?
List<Customer> customer = Arrays.stream(customerList).map(
s->{
String[] items = s.split(",");
return new Customer(items[0], items[1], items[2]);
}
}.collect(Collectors.toList());
答案 3 :(得分:1)
我认为这是您想要实现的目标,
String[] csv = {" jimmy ,johnson,jjohnson@gmail.com",
"Joe,Donald,Joe_Donald@donald.org",
"ARTHUR,THOMPSON,ARTHUR@thompson.org"};
Customer[] customs = new Customer[csv.length];
for (int i = 0; i < csv.length ; i++) {
String[] customerDetails = csv[i].split(",");
customs[i] = new Customer(customerDetails[0].trim(), customerDetails[1].trim(), customerDetails[2].trim());
}
System.out.println(customs[0].getFirst_name()));
System.out.println(customs[0].getLast_name());
System.out.println(customs[0].getEmail());
答案 4 :(得分:1)
我将从覆盖toString
中的Customer
开始。您没有发布Customer
版本,但这看起来像
public class Customer {
private String firstName;
private String lastName;
private String email;
public Customer(String first, String last, String email) {
this.firstName = first.trim();
this.lastName = last.trim();
this.email = email.trim();
}
@Override
public String toString() {
return String.format("first: %s, last: %s, email: %s", firstName, lastName, email);
}
}
然后,您可以使用String.split
和Arrays.stream
并将条目映射到Customer
实例,例如
String[] csv = { " jimmy ,johnson,jjohnson@gmail.com", "Joe,Donald,Joe_Donald@donald.org",
"ARTHUR,THOMPSON,ARTHUR@thompson.org" };
List<Customer> customs = Arrays.stream(csv).map(s -> s.split("\\s*,\\s*"))
.map(t -> new Customer(t[0], t[1], t[2])).collect(Collectors.toList());
for (Customer c : customs) {
System.out.println(c);
}
然后我得到
first: jimmy, last: johnson, email: jjohnson@gmail.com
first: Joe, last: Donald, email: Joe_Donald@donald.org
first: ARTHUR, last: THOMPSON, email: ARTHUR@thompson.org
答案 5 :(得分:1)
以下是对您的建议,需要指出以下几点:
示例代码:
public static void main(String[] args) throws IOException {
String[] csv = {
" jimmy ,johnson,jjohnson@gmail.com",
"Joe,Donald,Joe_Donald@donald.org",
"ARTHUR,THOMPSON,ARTHUR@thompson.org"
};
// use a List rather than array, so it can grow dynamically
List<Customer> customers = new ArrayList<Customer>();
for (String line : csv) {
System.out.println("Processing line: " + line);
String[] parts = line.split(",");
if (parts.length != 3) {
System.out.println("Expected to find 3 parts in the line, but got " + parts.length);
}
// construct the customer, notice the .trim() to remove any whitespace
Customer customer = new Customer(parts[0].trim(), parts[1].trim(), parts[2].trim());
customers.add(customer);
}
System.out.println("Printing out customer list:");
// loop through the customers and print them out
for (Customer c : customers) {
System.out.println("firstName: " + c.firstName);
System.out.println("lastName: " + c.lastName);
System.out.println("email: " + c.email);
System.out.println("\n");
}
}
static class Customer {
// accessors removed, consider using Lombok for @Data, @Getter, @Setter etc
String firstName;
String lastName;
String email;
public Customer(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
}
这是我得到的输出,我相信这就是您想要的
Processing line: jimmy ,johnson,jjohnson@gmail.com
Processing line: Joe,Donald,Joe_Donald@donald.org
Processing line: ARTHUR,THOMPSON,ARTHUR@thompson.org
Printing out customer list:
firstName: jimmy
lastName: johnson
email: jjohnson@gmail.com
firstName: Joe
lastName: Donald
email: Joe_Donald@donald.org
firstName: ARTHUR
lastName: THOMPSON
email: ARTHUR@thompson.org
祝你好运!
答案 6 :(得分:1)
我认为您最好的选择是将csv阵列的每个元素都视为一个不同的客户。无需将它们全部连接成一个大字符串。
String[] csv = {" jimmy ,johnson,jjohnson@gmail.com",
"Joe,Donald,Joe_Donald@donald.org",
"ARTHUR,THOMPSON,ARTHUR@thompson.org"};
Customer[] customs = new Customer[csv.length];
for (int cidx = 0; cidx < csv.length; cidx++) {
String[] fields = csv[cidx].split(",");
customs[cidx++] = new Customer(
fields.length>0 ? fields[0].trim() : null,
fields.length>1? fields[1].trim() : null,
fields.length>2? fields[2].trim() : null);
}
for (Customer custom : customs) {
System.out.println("first="+custom.getFirst_name() + ", last="+custom.getLast_name()+", email="+custom.getEmail());
}