填充对象的链接列表(当程序为 运行 时没有任何反应)
Filling a linked list of objects (Nothing happens when program is run)
我正在尝试创建客户对象的链接列表,但我不完全确定如何填充该列表。当我 运行 程序没有任何反应并且程序似乎卡在了 while 循环中?我真的不知道
这是我的客户class
public class Customer {
private String name;
private String address;
private String num;
private int year;
public Customer(String name, String address, String num, int year) {
this.name = name;
this.address = address;
this.num = num;
this.year = year;
}
@Override
public String toString() {
return "Name: " + name + "\nAddress: " + address + "\nCustomer Number: " + num + "\nLast Order: " + year;
}
}
这是我的测试员 class:
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
import java.util.*;
public class CustomerRunner {
public static void main(String[] args) throws FileNotFoundException {
String name;
String address;
String num;
int year;
Customer customer;
LinkedList<Customer> lst = new LinkedList<Customer>();
Scanner in = new Scanner(new File("Customers.txt"));
Scanner input = new Scanner(System.in);
while(in.hasNext()) {
for (int i = 0; i < lst.size(); i++){
name = in.next();
address = in.next();
num = in.next();
year = in.nextInt();
customer = new Customer(name, address, num, year);
System.out.println(customer.toString());
//fill linkedList
lst.add(customer);
}
}
System.out.println("1");
System.out.println(lst.size());
}
}
println(1) 和 println(lst.size()) 只是为了尝试获得某种输出。
供参考,这是我的 Customers.txt
Brooke MainStreet 123456789 2016
Adam MainStreet 234567890 2015
Zack MainStreet 123412341 2014
Cam MainStreet 453648576 2010
Tanya MainStreet 121212121 2005
Jason MainStreet 556574958 2004
Andrew MainStreet 777777777 2012
John MainStreet 999999999 2015
Jeff MainStreet 555444321 2010
你的 for 循环永远不会循环。 ArrayList lst 最初没有任何项目,因此当 for 循环 运行s 时它的大小为 0。外部 while 循环不断重复,因为 Scanner 永远不会在 for 循环失败时放弃任何标记。相反,根本不要使用 for 循环,而只是使用 while 循环来检查 Scanner 对象何时有 运行 个标记。
例如,
while (in.hasNextLine()) {
String line = in.nextLine();
// I scan here the line obtained, but also could split the String
// if desired
Scanner lineScanner = new Scanner(line);
String name2 = lineScanner.next();
String addr2 = lineScanner.next();
String num2 = lineScanner.next();
int year2 = lineScanner.nextInt();
lineScanner.close();
Customer cust2 = new Customer(name2, addr2, num2, year2);
lst.add(cust2);
}
我正在尝试创建客户对象的链接列表,但我不完全确定如何填充该列表。当我 运行 程序没有任何反应并且程序似乎卡在了 while 循环中?我真的不知道
这是我的客户class
public class Customer {
private String name;
private String address;
private String num;
private int year;
public Customer(String name, String address, String num, int year) {
this.name = name;
this.address = address;
this.num = num;
this.year = year;
}
@Override
public String toString() {
return "Name: " + name + "\nAddress: " + address + "\nCustomer Number: " + num + "\nLast Order: " + year;
}
}
这是我的测试员 class:
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
import java.util.*;
public class CustomerRunner {
public static void main(String[] args) throws FileNotFoundException {
String name;
String address;
String num;
int year;
Customer customer;
LinkedList<Customer> lst = new LinkedList<Customer>();
Scanner in = new Scanner(new File("Customers.txt"));
Scanner input = new Scanner(System.in);
while(in.hasNext()) {
for (int i = 0; i < lst.size(); i++){
name = in.next();
address = in.next();
num = in.next();
year = in.nextInt();
customer = new Customer(name, address, num, year);
System.out.println(customer.toString());
//fill linkedList
lst.add(customer);
}
}
System.out.println("1");
System.out.println(lst.size());
}
}
println(1) 和 println(lst.size()) 只是为了尝试获得某种输出。
供参考,这是我的 Customers.txt
Brooke MainStreet 123456789 2016
Adam MainStreet 234567890 2015
Zack MainStreet 123412341 2014
Cam MainStreet 453648576 2010
Tanya MainStreet 121212121 2005
Jason MainStreet 556574958 2004
Andrew MainStreet 777777777 2012
John MainStreet 999999999 2015
Jeff MainStreet 555444321 2010
你的 for 循环永远不会循环。 ArrayList lst 最初没有任何项目,因此当 for 循环 运行s 时它的大小为 0。外部 while 循环不断重复,因为 Scanner 永远不会在 for 循环失败时放弃任何标记。相反,根本不要使用 for 循环,而只是使用 while 循环来检查 Scanner 对象何时有 运行 个标记。
例如,
while (in.hasNextLine()) {
String line = in.nextLine();
// I scan here the line obtained, but also could split the String
// if desired
Scanner lineScanner = new Scanner(line);
String name2 = lineScanner.next();
String addr2 = lineScanner.next();
String num2 = lineScanner.next();
int year2 = lineScanner.nextInt();
lineScanner.close();
Customer cust2 = new Customer(name2, addr2, num2, year2);
lst.add(cust2);
}