简体   繁体   English

实施扫描仪以将文本文件读取到数组

[英]Implementing Scanner to read text file to array

For this program, what i have written so far is in the code below. 对于此程序,我到目前为止编写的内容在下面的代码中。 I am stuck. 我被困住了。 Any hints/help would be greatly appreciated. 任何提示/帮助将不胜感激。

Given the UML Class Diagram and the TestApp class, produce a working system that meets the following requirements: 给定UML类图和TestApp类,生成一个满足以下要求的工作系统:

Implement a Department class with toString and equals overrides. 使用toString和equals重写实现Department类。 Two department objects are equal if both the name and building numbers match. 如果名称和建筑物编号匹配,则两个部门对象相等。

Create an abstract Employee class with toString and equals overrides. 使用toString和equals覆盖创建一个抽象的Employee类。 Two abstract employees are considered to be the same if they are in the same department. 如果两个抽象雇员在同一部门,则认为他们是相同的。

Implement a Consultant class that has an hourlySalary. 实施具有小时工资的顾问类。 Two consultants are equal if they have the same rate (in pennies). 如果两个顾问的费率相同(以美分计),则他们是相等的。

Implement a SalariedEmployee class. 实现SalariedEmployee类。 Salaried employees are equal if they are in the same department and have the same salary (in pennies). 如果在同一部门并且工资相同(以美分计),则受薪雇员是平等的。

Follow the TODO instructions in the TestApp class to read the departments from the input txt file and test the system. 按照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);
}

}

* Updated TestApp *更新了TestApp

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

You're calling get(0) and get(1) on departments before putting anything into the ArrayList. 在将任何内容放入ArrayList之前,您要在部门上调用get(0)get(1) Solution: don't do this! 解决方案:不要这样做! Fill the ArrayList and don't call get(...) ever on it before knowing that something exists at that index. 填充ArrayList并在知道该索引处存在某些内容之前不要对其调用get(...)

populateEmployeeArray(company, depts);  // passed into method

And here's how you fill the depts ArrayList: 这是填充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;
}

As you can see, this code does nothing but returns an empty ArrayList, and so it shows that you're going about things a bit backwards. 如您所见,这段代码除了返回一个空的 ArrayList之外什么也不做,因此它表明您正在做一些事。 You will instead want to fix this part of your program, get it to read in the text file first , and then fix the displayDepartments(...) method so you can test that the readDepartmentsFromFile works before trying to use the depts ArrayList! 实际上,您将需要修改你的程序的一部分,让它在文本文件中首先读取,然后修复displayDepartments(...)方法,这样你就可以测试该readDepartmentsFromFile试图使用ArrayList中科指南之前的作品!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM