[英]How to add in a read object to an ArrayList and print out user requested info
对于类,我们必须读取一个包含状态,其缩写和填充的CSV文件,并将对象添加到数组列表中。 那么我们必须以表格格式打印列表。 从那里我们必须询问用户他们正在寻找什么缩写,并打印出缩写,状态和填充。
我已经尝试过将这些对象添加到数组列表中,这是我的教授向我展示的方式,这破坏了整个程序。 从现在的设置来看,这是我从作业描述中获得的最接近的信息。 我们之前从未使用过数组列表,因此如果我错过了明显的内容,则必须原谅我。 我尝试在不同的地方添加for循环,以查看它是否可以正常工作,到目前为止还没有。 我迷失了如何在数据中查找特定项目并将其拉出。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
class Reader {
public static void main(String[] args) {
String abbreviation = "";
String nameOfState = "";
int population = 0;
Scanner inFile = null;
// open file
try {
inFile = new Scanner(new File("States.txt"));
} catch (FileNotFoundException e) {
System.out.println("Error: File not found");
}
// read file
System.out.printf("%-12s %5s %5s\n", "Abbreviation", "State Name",
"Pop");
System.out.printf("%-12s %5s %5s\n", "------------", "----------",
"---");
while (inFile.hasNext()) {
String record = inFile.nextLine(); // read up to new line
String[] tokens = record.split(",[ ]*");
abbreviation = tokens[0];
nameOfState = tokens[1];
population = Integer.parseInt(tokens[2]);
System.out.printf("%-12s %5s %12d\n", abbreviation, nameOfState,
population);
}
Scanner in = new Scanner(System.in);
System.out.println("Which state would you like to find?");
String question = in.nextLine();
for (int i = 0; i < abbreviation.length(); i++) {
if (question.equalsIgnoreCase(abbreviation)) {
} else {
System.out.println("Incorrect formating");
}
}
inFile.close();
}
}
-
public class States {
private String abbreviation;
private String nameOfState;
private int population;
public States(String abbreviation, String nameOfState, int population) {
super();
this.abbreviation = abbreviation;
this.nameOfState = nameOfState;
this.population = population;
}
public String getAbbreviation() {
return abbreviation;
}
public String getNameOfState() {
return nameOfState;
}
public int getPopulation() {
return population;
}
}
首先,让我们CAL类State
,而不是States
它有没有意义
那你需要
List<State> stateList = new ArrayList<>();
State
实例,并将其添加到列表中 List<State> stateList = new ArrayList<>();
while (inFile.hasNext()) {
String record = inFile.nextLine(); // read up to new line
String[] tokens = record.split(",[ ]*");
String abbreviation = tokens[0];
String nameOfState = tokens[1];
int population = Integer.parseInt(tokens[2]);
stateList.add(new State(abbreviation, nameOfState, population));
}
然后遍历列表中的每个State
,如果缩写与用户输入匹配:将其打印出来
Scanner in = new Scanner(System.in);
System.out.println("Which state would you like to find?");
String question = in.nextLine();
for (State state : stateList) {
if (question.equalsIgnoreCase(state.getAbbreviation())) {
System.out.println(state);
}
}
而在State
班级
@Override
public String toString() {
return String.format("%-12s %5s %12d", abbreviation, nameOfState, population);
}
流媒体版本的乐趣
使用直接接受该行的构造函数
public State(String data) {
String[] tokens = data.split(",\\s*");
this.abbreviation = tokens[0];
this.nameOfState = tokens[1];
this.population = Integer.parseInt(tokens[2]);
}
你可以
List<State> stateList = Files.lines(Path.of("States.txt"))
.map(State::new)
.collect(Collectors.toList());
stateList.stream()
.filter(state -> state.getAbbreviation().equals(question))
.forEach(System.out::println);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.