我是Java的新手,我正在构建一个项目,试图解析与用户交互的数据。 感到困惑的是,我解析的数据仍然保留在解析方法中,我不知道如何正确地创建对象并使用它。 我有三个类ethicalengine
,场景
,audit
来处理数据。 我会让它尽可能的简单,任何帮助都是高度赞赏的。
我以前犯过这个错误如何使用ArrayList setter加载对象
但我仍然感到困惑,因为引用越来越复杂。
这里是我的主要类EthicalEngine
的一部分,用于加载数据和用户交互。
private Audit audit = new Audit();
private Scenario scenario = new Scenario();
private ArrayList<Scenario> scenarios = new ArrayList<Scenario>();
private ArrayList<Character> passengers = new ArrayList<Character>();
private ArrayList<Character> pedestrians = new ArrayList<Character>();
public void loadCsv() throws IOException {
// detail of parsing data to program
try {
//parsing data and save it to the proper ArrayList
} else {
lineCount++;
throw new InvalidDataFormatException();
}
} catch (InvalidDataFormatException e) {
System.out.println("WARNING: invalid data format in config file in line " + lineCount);
}
}
// add the last round data
// parsing data like this pattern
scenario.setPassengers(passengers);
scenario.setPedestrians(pedestrians);
scenarios.add(scenario);
scenario.setPassengers(passengers);
scenario.setPedestrians(pedestrians);
// use the audit object in the field area
// trying to construct a new object, but failed
audit = new Audit(scenarios);
//data only survive here!!
return;
} catch (FileNotFoundException e) {
System.out.println("ERROR: could not find config file.");
}
}
public void interactConfig () throws IOException {
//cannot get the loading data
ArrayList<Scenario> scenarios = audit.getScenarios();
ArrayList<Character> passengers = new ArrayList<Character>();
ArrayList<Character> pedestrians = new ArrayList<Character>();
do {
for (Scenario scenario : scenarios) {
audit.setAuditType("User Audit");
passengers = scenario.getPassengers();
pedestrians = scenario.getPedestrians();
System.out.println(scenario.toString());
//some interaction with the Scenario and the Scanner class
System.out.println("Would you like to continue? (yes/no)");
playAgain = in.nextLine();
} while (playAgain.equals("yes"));
}
以下是我的Audit
类的一部分:
public class Audit {
/**
* default constructor
*/
public Audit() {
}
/**
* The constructor to get the ArrayList<Scenario>
* @param ArrayList<Scenario>
*/
public Audit(ArrayList<Scenario> scenarios) {
this.scenarios = scenarios;
}
以下是我的方案
类的一部分:
public class Scenario {
private Random random;
private ArrayList<Character> passengers = new ArrayList<Character>();
private ArrayList<Character> pedestrians = new ArrayList<Character>();
private boolean legalCrossing;
/**
* default constructor
*/
public Scenario() {
}
/**
* Construct the scenario objects
*
* @param passengers arrayList
* @param pedestrians arrayList
* @param legalCrossing boolean
*/
public Scenario(ArrayList<Character> passengers, ArrayList<Character> pedestrians, boolean legalCrossing) {
this.passengers = passengers;
this.pedestrians = pedestrians;
this.legalCrossing = legalCrossing;
}
/**
* The setter method to set the passenger arrayList
* @param ArrayList<Charater> passengers
*/
public void setPassengers(ArrayList<Character> passengers) {
this.passengers = passengers;
}
/**
* The getter method to get the passengers arrayList
* @return ArrayList<Character> passengers
*/
public ArrayList<Character> getPassengers() {
return passengers;
}
/**
* The setter method to set the pedestrians arrayList
* @param ArrayList<Chracter> pedestrians
*/
public void setPedestrians(ArrayList<Character> pedestrians) {
this.pedestrians = pedestrians;
}
/**
* The getter method to get the pedestrian arrayList
* @return pedestrian arrayList
*/
public ArrayList<Character> getPedestrians() {
return pedestrians;
}
您应该调用loadCSV()
方法来解析数据并初始化Private Audit Audit
字段。 然后,您可以在public void interactConfig()
方法中使用它。