所以我有一个名为“phone.txt”的文本文件,我将它加载到一个arraylist中,问题是现在我不知道如何在同一个类的方法中使用该arraylist。让我们说我的方法“optionP”,我希望客户能够在该arraylist中搜索一个名称,并显示该人的信息,我该怎么做?到目前为止我的代码是这样的:
import java.util.*;
import java.io.*;
public class Directory {
Scanner kbd = new Scanner(System.in);
ArrayList<Person> persons = new ArrayList<Person>();
public void run() throws FileNotFoundException {
String firstName;
String lastName;
String initial;
String department;
int telNum;
File inFile = new File("phone.txt");
Scanner in = new Scanner(inFile);
while (in.hasNext()) {
Person list;
lastName = in.next();
firstName = in.next();
initial = in.next();
department = in.next();
telNum = in.nextInt();
list = new Person(lastName, firstName, initial, department, telNum);
persons.add(list);
}
in.close();
int i;
i = 0;
while (i < persons.size()) {
System.out.println(persons.get(i).toString());
i++;
}
char userInput = kbd.next().charAt(0);
if (userInput == 'p' || userInput == 'P') {
optionP();
} else if (userInput == 'l' || userInput == 'L') {
optionL();
} else if (userInput == 'r' || userInput == 'R') {
optionR();
} else if (userInput == 'c' || userInput == 'C') {
optionC();
} else if (userInput == 'a' || userInput == 'A') {
optionA();
} else {
optionD();
}
}
public void optionP() {
}
public void optionL() {
}
public void optionR() {
}
public void optionC() {
}
public void optionA() {
}
public void optionD() {
}
public class Person {
String firstName;
String lastName;
String initial;
String department;
int telNum;
public Person(String firstName, String lastName, String initial, String department, int telNum) {
this.firstName = firstName;
this.lastName = lastName;
this.initial = initial;
this.department = department;
this.telNum = telNum;
}
}
}
答案 0 :(得分:0)
我猜这个文件有数据,你可以在控制台上打印时看到数据,如果没有,你必须先修复它
其次,如果你想从arrayList中读取
for(Person p:persons){
//do what you want
}
答案 1 :(得分:0)
OptionP
需要浏览ArrayList
并找到相应的结果
此外,因为您需要迭代ArrayList,所以最好使用LinkedList
。
您可能还需要添加toString
方法来描述结果。
import java.util.*;
import java.io.*;
public class Directory {
Scanner kbd = new Scanner(System.in);
List<Person> persons = new LinkedList<Person>();
public void run() throws FileNotFoundException {
String firstName;
String lastName;
String initial;
String department;
int telNum;
File inFile = new File("/Users/zhaojing/Desktop/phone.txt");
Scanner in = new Scanner(inFile);
while (in.hasNext()) {
Person list;
lastName = in.next();
firstName = in.next();
initial = in.next();
department = in.next();
telNum = in.nextInt();
list = new Person(lastName, firstName, initial, department, telNum);
persons.add(list);
}
in.close();
int i;
i = 0;
while (i < persons.size()) {
i++;
}
char userInput = kbd.next().charAt(0);
if (userInput == 'p' || userInput == 'P') {
optionP();
} else if (userInput == 'l' || userInput == 'L') {
optionL();
} else if (userInput == 'r' || userInput == 'R') {
optionR();
} else if (userInput == 'c' || userInput == 'C') {
optionC();
} else if (userInput == 'a' || userInput == 'A') {
optionA();
} else {
optionD();
}
}
public Person optionP() {
String userInputFirstName = kbd.next();
for (Person p : persons) {
if (Objects.equals(userInputFirstName, p.firstName)) {
System.out.println(p.toString());
return p;
}
}
return null;
}
public void optionL() {
}
public void optionR() {
}
public void optionC() {
}
public void optionA() {
}
public void optionD() {
}
public class Person {
String firstName;
String lastName;
String initial;
String department;
int telNum;
public Person(String firstName, String lastName, String initial, String department, int telNum) {
this.firstName = firstName;
this.lastName = lastName;
this.initial = initial;
this.department = department;
this.telNum = telNum;
}
@Override
public String toString() {
return "result: Person{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", initial='" + initial + '\'' +
", department='" + department + '\'' +
", telNum=" + telNum +
'}';
}
}
}