[英]java: Read text file and store the info in an array using scanner class
[英]Implementing Scanner to read text file to array
對於此程序,我到目前為止編寫的內容在下面的代碼中。 我被困住了。 任何提示/幫助將不勝感激。
給定UML類圖和TestApp類,生成一個滿足以下要求的工作系統:
使用toString和equals重寫實現Department類。 如果名稱和建築物編號匹配,則兩個部門對象相等。
使用toString和equals覆蓋創建一個抽象的Employee類。 如果兩個抽象雇員在同一部門,則認為他們是相同的。
實施具有小時工資的顧問類。 如果兩個顧問的費率相同(以美分計),則他們是相等的。
實現SalariedEmployee類。 如果在同一部門並且工資相同(以美分計),則受薪雇員是平等的。
按照TestApp類中的TODO指令從輸入的txt文件中讀取部門並測試系統。
Ouput should be:
Executive 100
Research 112
Engineering 115
Production 120
Accounting 122
Executive 100(100) Steve Jobs $1000000.00
Research 112(200) Bill Joy $300000.00
Engineering 115(300) James Gosling $300000.00
Production 120(1100) Craig McClanahan $250.00
Accounting 122(400) Kevin Malone $51567.00
Research 112
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class TestApp {
public static void main(String[] args) {
ArrayList<Department> depts =
readDepartmentsFromFile("depts.txt");
displayDepartments(depts);
ArrayList<Employee> company = new ArrayList<>();
populateEmployeeArray(company, depts);
// TODO: Display everyone's pay w/o referring to the
// subclasses (i.e., using Employee only)
// TODO: Display all departments that are equal to this one:
Department d1 = new Department("Research", 112);
// TODO: Display all departments that are equal to this one:
Department d2 = new Department("Accounting", 50);
}
private static void displayDepartments(ArrayList<Department> list) {
// TODO: Display the list to std error (NOT standard output)
}
private static ArrayList<Department> readDepartmentsFromFile(String filename) {
ArrayList<Department> list = new ArrayList<>();
// TODO: Use a Scanner to read the input file depts.txt
// and create one Department object per line in the file.
// Add all objects to the list.
// Be sure to close the scanner object using a finally block.
// Handle possible execptions by producing a nice error
// along with the execption's message to standard error
// (NOT standard output).
return list;
}
public static void populateEmployeeArray(ArrayList<Employee> array,
ArrayList<Department> departments) {
SalariedEmployee exec = new SalariedEmployee(100, "Steve",
"Jobs", departments.get(0), 100000000);
SalariedEmployee architect = new SalariedEmployee(200, "Bill",
"Joy", departments.get(1), 30000000);
SalariedEmployee engineer = new SalariedEmployee(300, "James",
"Gosling", departments.get(2), 30000000);
Consultant consultant = new Consultant(1100, "Craig",
"McClanahan", departments.get(3), 25000);
SalariedEmployee accountant = new SalariedEmployee(400,
"Kevin", "Malone", departments.get(4), 5156700);
array.add(exec);
array.add(architect);
array.add(engineer);
array.add(consultant);
array.add(accountant);
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class TestApp {
public static void main(String[] args) {
ArrayList<Department> depts =
readDepartmentsFromFile("depts.txt");
displayDepartments(depts);
ArrayList<Employee> company = new ArrayList<>();
populateEmployeeArray(company, depts);
// TODO: Display everyone's pay w/o referring to the
// subclasses (i.e., using Employee only)
for (Employee employee : company){
System.out.println(employee);
}
// TODO: Display all departments that are equal to this one:
Department d1 = new Department("Research", 112);
for (Department department : depts)
if(department.equals(d1)){
System.out.println(department);
}
// TODO: Display all departments that are equal to this one:
Department d2 = new Department("Accounting", 50);
for(Department department : depts){
if(department.equals(d2)){
System.out.println(department);
}
}
}
private static void displayDepartments(ArrayList<Department> list) {
// TODO: Display the list to std error (NOT standard output)
System.err.println(department.toString());
}
private static ArrayList<Department> readDepartmentsFromFile(String filename) {
ArrayList<Department> list = new ArrayList<>();
// TODO: Use a Scanner to read the input file depts.txt
// and create one Department object per line in the file.
// Add all objects to the list.
// Be sure to close the scanner object using a finally block.
// Handle possible execptions by producing a nice error
// along with the execption's message to standard error
// (NOT standard output).
File aFile = new File(filename);
System.out.println(aFile.exists());
Scanner sc = null;
if (aFile.exists())
try{
sc = new Scanner(aFile);
while(sc.hasNext()) {
String deptName = sc.next();
int buildNum = sc.nextInt();
Department mallory = new Department(deptName, buildNum);
list.add(mallory);
}
}
catch(FileNotFoundException exec) {
System.out.println(exec.getMessage());
}
finally {
sc.close();
}
return list;
}
public static void populateEmployeeArray(ArrayList<Employee> array,
ArrayList<Department> departments) {
SalariedEmployee exec = new SalariedEmployee(100, "Steve",
"Jobs", departments.get(0), 100000000);
SalariedEmployee architect = new SalariedEmployee(200, "Bill",
"Joy", departments.get(1), 30000000);
SalariedEmployee engineer = new SalariedEmployee(300, "James",
"Gosling", departments.get(2), 30000000);
Consultant consultant = new Consultant(1100, "Craig",
"McClanahan", departments.get(3), 25000);
SalariedEmployee accountant = new SalariedEmployee(400,
"Kevin", "Malone", departments.get(4), 5156700);
array.add(exec);
array.add(architect);
array.add(engineer);
array.add(consultant);
array.add(accountant);
}
}
TEXT FILE -
depts.txt -
Executive 100
Research 112
Engineering 115
Production 120
Accounting 122
在將任何內容放入ArrayList之前,您要在部門上調用get(0)
和get(1)
。 解決方案:不要這樣做! 填充ArrayList並在知道該索引處存在某些內容之前不要對其調用get(...)
。
populateEmployeeArray(company, depts); // passed into method
這是填充depts ArrayList的方法:
// you might need a bit more code in here!!!! The // TODO: means do it :)
private static ArrayList<Department> readDepartmentsFromFile(String filename) {
ArrayList<Department> list = new ArrayList<>();
// TODO: Use a Scanner to read the input file depts.txt
// and create one Department object per line in the file.
// Add all objects to the list.
// Be sure to close the scanner object using a finally block.
// Handle possible execptions by producing a nice error
// along with the execption's message to standard error
// (NOT standard output).
return list;
}
如您所見,這段代碼除了返回一個空的 ArrayList之外什么也不做,因此它表明您正在做一些事。 實際上,您將需要修改你的程序的一部分,讓它在文本文件中首先讀取,然后修復displayDepartments(...)
方法,這樣你就可以測試該readDepartmentsFromFile試圖使用ArrayList中科指南之前的作品!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.