错误指出“类型不兼容:类无法转换为数组”

时间:2019-03-19 02:49:48

标签: java arrays class incompatibletypeerror

我的目标是从.csv文件中提取数据,并创建一个带有交互式菜单的数组以查找某些信息。我已经在这段代码上工作了几天。我遇到的唯一问题是此错误说明:“ CrimeDataClass无法转换为CrimeDataArray”。我已经看了2-3天了。我觉得这很简单。预先感谢您的帮助。

测试程序

package crimedata;

import java.time.Duration;
import java.time.Instant;
import java.util.Scanner;

public class TestCrimeData {

public static void main(String[] args) {

    // Start time
    Instant start = Instant.now();
    String fileName = ("");

    // Command Line Argument
    try {
        if (args.length == 1) {
            fileName = args[0];
        } // End If
        else {
            System.out.println("Require Crime.csv");
        } // End Else

    } // End Try
    catch (Exception e) {
        System.out.println("Try Again");

    } // End Catch 

    // Create Data array
    CrimeDataClass myCrimes = new CrimeDataClass(); 

这是我得到错误的地方。

    // Interaction menu
    menu(myCrimes);

其余代码没有错误。

    // End time
    Instant end = Instant.now();
    System.out.println("Elapsed time (seconds): "
            + Duration.between(start, end).toNanos() / 1_000_000_000.0);

} // End Main

private static void menu(CrimeDataArray crimes) {

    // Interaction with data
    Scanner scannerIn = new Scanner(System.in);

    System.out.println("********** Welcome to the US Crime Statistical Application **************************");
    while (true) {
        System.out.println("");
        System.out.println("\nEnter the number of the question you want answered. Enter ‘Q’ to quit the program.\n"
                + "\n1. What were the percentages in population growth for each consecutive year from 1994 – 2013?"
                + "\n2. What year had the highest Violent crime rate?"
                + "\n3. What year had the lowest Violent crime rate?"
                + "\n4. What year had the highest Murder rate?"
                + "\n5. What year had the lowest Murder rate?"
                + "\n6. What year had the highest Robbery rate?"
                + "\n7. What year had  the lowest Robbery rate?"
                + "\n8. What year had the highest Rape rate?"
                + "\n9. What year had  the lowest Rape rate?"
                + "\n10. What year had the highest Aggravated Assault rate?"
                + "\n11. What year had  the lowest Aggravated Assault rate?"
                + "\n12. What year had the highest Property crime rate?"
                + "\n13. What year had  the lowest Property crime rate?"
                + "\n14. What year had the highest Burglary rate?"
                + "\n15. What year had  the lowest Burglary rate?"
                + "\n16. What year had the highest Larceny theft rate?"
                + "\n17. What year had  the lowest Larceny theft rate?"
                + "\n18. What year had the highest Motor Vehicle theft rate?"
                + "\n19. What year had  the lowest Motor Vehicle theft rate?"
                + "\n20. What are the total crimes commited for each year from 1994 – 2013?"
                + "\nQ. Quit program."
                + "\nEnter your selection:");

        // Read next line
        String selection = scannerIn.nextLine().trim().toUpperCase();
        if (selection.equals("Q")) {
            System.out.println("Thank you for trying the US Crimes Statistics Program.");
            // End loop when Q is entered
            break;
        } // End If
        else {
            switch (selection) {
                case "1":
                    crimes.getPopulationGrowth();
                    break;
                case "2":
                    System.out.printf("The Violent Crime rate was highest in %d", crimes.getMaxViolentCrimeRate());
                    break;
                case "3":
                    System.out.printf("The Violent Crime rate was lowest in %d", crimes.getMinViolentCrimeRate());
                    break;
                case "4":
                    System.out.printf("The Murder rate was highest in %d", crimes.getMaxMurderRate());
                    break;
                case "5":
                    System.out.printf("The Murder rate was lowest in %d", crimes.getMinMurderRate());
                    break;
                case "6":
                    System.out.printf("The Rape rate was highest in %d", crimes.getMaxRapeRate());
                    break;
                case "7":
                    System.out.printf("The Rape rate was lowest in %d", crimes.getMinRapeRate());
                    break;
                case "8":
                    System.out.printf("The Robbery rate was highest in %d", crimes.getMaxRobberyRate());
                    break;
                case "9":
                    System.out.printf("The Robbery rate was lowest in %d", crimes.getMinRobberyRate());
                    break;
                case "10":
                    System.out.printf("The Aggravated Assault rate was highest in %d", crimes.getMaxAggravatedAssaultRate());
                    break;
                case "11":
                    System.out.printf("The Aggravated Assault rate was lowest in %d", crimes.getMinAggravatedAssaultRate());
                    break;
                case "12":
                    System.out.printf("The Aggravated Assault rate was highest in %d", crimes.getMaxPropertyCrimeRate());
                    break;
                case "13":
                    System.out.printf("The Aggravated Assault rate was lowest in %d", crimes.getMinPropertyCrimeRate());
                    break;
                case "14":
                    System.out.printf("The Burglary rate was highest in %d", crimes.getMaxBurglaryRate());
                    break;
                case "15":
                    System.out.printf("The Burglary rate was lowest in %d", crimes.getMinBurglaryRate());
                    break;
                case "16":
                    System.out.printf("The Larceny theft was rate was highest in %d", crimes.getMaxLarcenyTheftRate());
                    break;
                case "17":
                    System.out.printf("The Larceny theft was lowest in %d", crimes.getMinLarcenyTheftRate());
                    break;
                case "18":
                    System.out.printf("The Motor Vehicle theft was rate was highest in %d", crimes.getMaxMotorVehicleTheftRate());
                    break;
                case "19":
                    System.out.printf("The Motor Vehicle theft was lowest in %d", crimes.getMinMotorVehicleTheftRate());
                case "20":
                    System.out.println(crimes.toString());
                    break;
                default:
                    System.out.println("Invalid selection. Please try again.");
                    break;

            } // End Switch

        } // End Else

    } // End While

    scannerIn.close();

} // End Menu

} // End Class

班级:

public class CrimeDataClass {

// Variable for the class
private int year = 1994;
private int population = 0;
private int violentCrime = 0;
private double violentCrimeRate = 0.0;
private int murder = 0;
private double murderRate = 0.0;
private int rape = 0;
private double rapeRate = 0.0;
private int robbery = 0;
private double robberyRate = 0.0;
private int aggravatedAssault = 0;
private double aggravatedAssaultRate = 0.0;
private int propertyCrime = 0;
private double propertyCrimeRate = 0.0;
private int burglary = 0;
private double burglaryRate = 0.0;
private int larcenyTheft = 0;
private double larcenyTheftRate = 0.0;
private int motorVehicleTheft = 0;
private double motorVehicleTheftRate = 0.0;

// Default constructor
public CrimeDataClass() {
} // End Constructor

// Full Parameterize Constructor
public CrimeDataClass(int year, int population, int violentCrime, double violentCrimeRate,
        int murder, double murderRate, int rape, double rapeRate, int robbery, double robberyRate,
        int aggravatedAssault, double aggravatedAssaultRate, int propertyCrime, double propertyCrimeRate,
        int burglary, double burglaryRate, int larcenyTheft, double larcenyTheftRate, int motorVehicleTheft,
        double motorVehicleTheftRate) {
    this.year = year;
    this.population = population;
    this.violentCrime = violentCrime;
    this.violentCrimeRate = violentCrimeRate;
    this.murder = murder;
    this.murderRate = murderRate;
    this.rape = rape;
    this.rapeRate = rapeRate;
    this.robbery = robbery;
    this.robberyRate = robberyRate;
    this.aggravatedAssault = aggravatedAssault;
    this.aggravatedAssaultRate = aggravatedAssaultRate;
    this.propertyCrime = propertyCrime;
    this.propertyCrimeRate = propertyCrimeRate;
    this.burglary = burglary;
    this.burglaryRate = burglaryRate;
    this.larcenyTheft = larcenyTheft;
    this.larcenyTheftRate = larcenyTheftRate;
    this.motorVehicleTheft = motorVehicleTheft;
    this.motorVehicleTheftRate = motorVehicleTheftRate;

} // End Constructor

// Getter Method
public int getYear() {
    return this.year;
}

public int getPopulation() {
    return this.population;
}

public int getViolentCrime() {
    return this.violentCrime;
}

public double getViolentCrimeRate() {
    return this.violentCrimeRate;
}

public int getMurder() {
    return this.murder;
}

public double getMurderRate() {
    return this.murderRate;
}

public int getRape() {
    return this.rape;
}

public double getRapeRate() {
    return this.rapeRate;
}

public int getRobbery() {
    return this.robbery;
}

public double getRobberyRate() {
    return this.robberyRate;
}

public int getAggravatedAssault() {
    return this.aggravatedAssault;
}

public double getAggravatedAssaultRate() {
    return this.aggravatedAssaultRate;
}

public int getPropertyCrime() {
    return this.propertyCrime;
}

public double getPropertyCrimeRate() {
    return this.propertyCrimeRate;
}

public int getBurglary() {
    return this.burglary;
}

public double getBurglaryRate() {
    return this.burglaryRate;
}

public int getLarcenyTheft() {
    return this.larcenyTheft;
}

public double getLarcenyTheftRate() {
    return this.larcenyTheftRate;
}

public int getMotorVehicleTheft() {
    return this.motorVehicleTheft;
}

public double getMotorVehicleTheftRate() {
    return this.motorVehicleTheftRate;
} // End Getter Method

// Setter Method
// Objects are unchangeable due to using values from CSV

//toString Method
public String toString() {
    return String.format("\nYear: %d"
            + "\nPopulation: %d"
            + "\nViolent Crime: %d"
            + "\nViolent Crime Rate: %.2f"
            + "\nMurder: %d"
            + "\nMurder Rate: %.2f"
            + "\nRape: %d"
            + "\nRape Rate: %.2f"
            + "\nRobbery: %d"
            + "\nRobbery Rate: %.2f"
            + "\nAggravated Assault: %d"
            + "\nAggravated Assault Rate: %.2f"
            + "\nProperty Crime: %d"
            + "\nProperty Crime Rate: %.2f"
            + "\nBurglary: %d"
            + "\nBurglary Rate: %.2f"
            + "\nLarceny-Theft: %d"
            + "\nLarceny-Theft Rate: %.2f"
            + "\nMotor Vehicle Theft: %d"
            + "\nMotor Vehicle Theft Rate: %.2f", this.year,
            this.population, this.violentCrime, this.violentCrimeRate,
            this.murder, this.murderRate, this.rape, this.rapeRate,
            this.robbery, this.robberyRate, this.aggravatedAssault,
            this.aggravatedAssaultRate, this.propertyCrime, this.propertyCrimeRate,
            this.burglary, this.burglaryRate, this.larcenyTheft, this.larcenyTheftRate,
            this.motorVehicleTheft, this.motorVehicleTheftRate);

} // End toString Method

} // End Class

数组:

package crimedata;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class CrimeDataArray {

private CrimeDataClass[] crimes;
// csv file read setup
static Scanner scannerIn = null;
static FileInputStream in = null;
static BufferedReader inputStream = null;
static String fileLocation = "Crimes.csv";

// Method for reading Crime.csv file
public static void readFile() throws IOException {
    int fileChar;
    String fileLine;
    in = new FileInputStream("Crime.csv");
    System.out.println("ReadIt File Contents");
    // Reads one char at a time
    while ((fileChar = in.read()) != -1) {
        // convert int to char
        System.out.print((char) fileChar);
    }
    // Separate the file output
    System.out.println("");
    System.out.println("Crime.csv File Contents using BufferedReader");
    // Use of Scanner and BufferedReader
    inputStream = new BufferedReader(new FileReader("Crime.csv"));
    scannerIn = new Scanner(inputStream);
    while (scannerIn.hasNext()) {
        if (scannerIn.hasNextInt()) {
            System.out.println(scannerIn.nextInt());
        }
        if (scannerIn.hasNextDouble()) {
            System.out.println(scannerIn.nextDouble());
        } else {
            scannerIn.next();
        }
    }
    // Separe the file output
    System.out.println("");
    // Use of
    inputStream = new BufferedReader(new FileReader("Crime.csv"));
    System.out.println("Crime.csv contents");
    // Read one Line using BufferedReader
    while ((fileLine = inputStream.readLine()) != null) {
        System.out.println(fileLine);
    }
}

// Method to calculates number of rows in data file
private static int countRows(String dataFile) {

    String fileLine;
    BufferedReader inputStream = null;
    int rowCount = 0;

    try {
        // Count rows in file  
        inputStream = new BufferedReader(new FileReader("Crime.csv"));
        while ((fileLine = inputStream.readLine()) != null) {
            rowCount++;
        } // End While

    } // End Try
    catch (IOException io) {
        System.out.println("File IO Exception" + io.getMessage());

    } // End Catch
    finally {
        // Need another catch for closing streams
        try {
            // Close the streams
            if (inputStream != null) {
                inputStream.close();
            } // End If

        } // End Try
        catch (IOException io) {
            System.out.println("Issue closing the Files" + io.getMessage());
        } // End Catch

    } // End Finally
    return rowCount;
} // End countRows

// getPopulationGrowth
public void getPopulationGrowth() {
    for (int i = 0; i < (crimes.length - 2); i++) {
        System.out.printf("\n%d - %d: %.4f%%", crimes[i].getYear(), crimes[i + 1].getYear(),
                ((crimes[i + 1].getPopulation() - crimes[i].getPopulation()) / (float) crimes[i].getPopulation()) * 100);
    } // End For
} // End getPopulationGrowth

// getMaxViolentCrimeRate
public int getMaxViolentCrimeRate() {
    double violentCrimeRate = crimes[0].getViolentCrimeRate();
    double max;
    int i;

    // Define max value
    max = violentCrimeRate;

    // Loop to find max value
    for (i = 0; i <= crimes.length; i++) {
        if (violentCrimeRate > max) {
            max = violentCrimeRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMaxViolentCrimeRate

// getMinViolentCrimeRate
public int getMinViolentCrimeRate() {
    double violentCrimeRate = crimes[0].getViolentCrimeRate();
    double min;
    int i;

    // Define min value
    min = violentCrimeRate;

    // Loop to find min value
    for (i = 0; i > crimes.length; i++) {
        if (violentCrimeRate < min) {
            min = violentCrimeRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMinViolentCrimeRate

// getMaxMurderRate
public int getMaxMurderRate() {
    double murderRate = crimes[0].getMurderRate();
    double max;
    int i;

    // Define max value
    max = murderRate;

    // Loop to find max value
    for (i = 0; i <= (crimes.length - 2); i++) {
        if (murderRate > max) {
            max = murderRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMaxMurderRate

// getMinMurderRate
public int getMinMurderRate() {
    double murderRate = crimes[0].getMurderRate();
    double min;
    int i;

    // Define max value
    min = murderRate;

    // Loop to find max value
    for (i = 0; i > crimes.length; i++) {
        if (murderRate < min) {
            min = murderRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMinMurderRate

// getMaxRapeRate
public int getMaxRapeRate() {
    double rapeRate = crimes[0].getRapeRate();
    double max;
    int i;

    // Define max value
    max = rapeRate;

    // Loop to find max value
    for (i = 0; i <= crimes.length; i++) {
        if (rapeRate > max) {
            max = rapeRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMaxRapeRate

// getMinRapeRate
public int getMinRapeRate() {
    double rapeRate = crimes[0].getRapeRate();
    double min;
    int i;

    // Define max value
    min = rapeRate;

    // Loop to find max value
    for (i = 0; i > crimes.length; i++) {
        if (rapeRate < min) {
            min = rapeRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMinRapeRate

// getMaxRobberyRate
public int getMaxRobberyRate() {
    double robberyRate = crimes[0].getRobberyRate();
    double max;
    int i;

    // Define max value
    max = robberyRate;

    // Loop to find max value
    for (i = 0; i <= crimes.length; i++) {
        if (robberyRate > max) {
            max = robberyRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMaxRobberyRate

// getMinRobberyRate
public int getMinRobberyRate() {
    double robberyRate = crimes[0].getRobberyRate();
    double min;
    int i;

    // Define max value
    min = robberyRate;

    // Loop to find max value
    for (i = 0; i > crimes.length; i++) {
        if (robberyRate < min) {
            min = robberyRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMinRobberyRate

// getMaxAggravatedAssaultRate
public int getMaxAggravatedAssaultRate() {
    double aggravatedAssaultRate = crimes[0].getAggravatedAssaultRate();
    double max;
    int i;

    // Define max value
    max = aggravatedAssaultRate;

    // Loop to find max value
    for (i = 0; i <= crimes.length; i++) {
        if (aggravatedAssaultRate > max) {
            max = aggravatedAssaultRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMaxAggravatedAssaultRate

// getMinAggravatedAssaultRate
public int getMinAggravatedAssaultRate() {
    double aggravatedAssaultRate = crimes[0].getAggravatedAssaultRate();
    double min;
    int i;

    // Define max value
    min = aggravatedAssaultRate;

    // Loop to find max value
    for (i = 0; i > crimes.length; i++) {
        if (aggravatedAssaultRate < min) {
            min = aggravatedAssaultRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMinAggravatedAssaultRate

// getMaxPropertyCrimeRate
public int getMaxPropertyCrimeRate() {
    double propertyCrimeRate = crimes[0].getPropertyCrimeRate();
    double max;
    int i;

    // Define max value
    max = propertyCrimeRate;

    // Loop to find max value
    for (i = 0; i <= crimes.length; i++) {
        if (propertyCrimeRate > max) {
            max = propertyCrimeRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMaxPropertyCrimeRate

// getMinPropertyCrimeRate
public int getMinPropertyCrimeRate() {
    double propertyCrimeRate = crimes[0].getPropertyCrimeRate();
    double min;
    int i;

    // Define max value
    min = propertyCrimeRate;

    // Loop to find max value
    for (i = 0; i > crimes.length; i++) {
        if (propertyCrimeRate < min) {
            min = propertyCrimeRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMinPropertyCrimeRate

// getMaxBurglaryRate
public int getMaxBurglaryRate() {
    double burglaryRate = crimes[0].getBurglaryRate();
    double max;
    int i;

    // Define max value
    max = burglaryRate;

    // Loop to find max value
    for (i = 0; i <= crimes.length; i++) {
        if (burglaryRate > max) {
            max = burglaryRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMaxBurglaryRate

// getMinBurglaryRate
public int getMinBurglaryRate() {
    double burglaryRate = crimes[0].getBurglaryRate();
    double min;
    int i;

    // Define max value
    min = burglaryRate;

    // Loop to find max value
    for (i = 0; i > crimes.length; i++) {
        if (burglaryRate < min) {
            min = burglaryRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMinBurglaryRate

// getMaxLarcenyTheftRate
public int getMaxLarcenyTheftRate() {
    double larcenyTheftRate = crimes[0].getLarcenyTheftRate();
    double max;
    int i;

    // Define max value
    max = larcenyTheftRate;

    // Loop to find max value
    for (i = 0; i <= crimes.length; i++) {
        if (larcenyTheftRate > max) {
            max = larcenyTheftRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMaxLarcenyTheftRate

// getMinLarcenyTheftRate
public int getMinLarcenyTheftRate() {
    double larcenyTheftRate = crimes[0].getLarcenyTheftRate();
    double min;
    int i;

    // Define max value
    min = larcenyTheftRate;

    // Loop to find max value
    for (i = 0; i > crimes.length; i++) {
        if (larcenyTheftRate < min) {
            min = larcenyTheftRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMinLarcenyTheftRate

// getMaxMotorVehicleTheftRate
public int getMaxMotorVehicleTheftRate() {
    double motorVehicleTheftRate = crimes[0].getMotorVehicleTheftRate();
    double max;
    int i;

    // Define max value
    max = motorVehicleTheftRate;

    // Loop to find max value
    for (i = 0; i <= crimes.length; i++) {
        if (motorVehicleTheftRate > max) {
            max = motorVehicleTheftRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMaxMotorVehicleTheftRate

// getMinMotorVehicleTheftRate
public int getMinMotorVehicleTheftRate() {
    double motorVehicleTheftRate = crimes[0].getMotorVehicleTheftRate();
    double min;
    int i;

    // Define max value
    min = motorVehicleTheftRate;

    // Loop to find max value
    for (i = 0; i > crimes.length; i++) {
        if (motorVehicleTheftRate < min) {
            min = motorVehicleTheftRate;
        } // End If
    } // End For
    return crimes[i].getYear();
} // End getMinMotorVehicleTheftRate

// toString
public String toString() {
    return Arrays.toString(this.crimes);
}

} // End Class

2 个答案:

答案 0 :(得分:0)

这是一个工作版本。我已经从您的代码中重写了有问题的/需要修复的部分。请注意,我使用Java 8流和lambda来保持代码简洁。

Crimes.csv 应该看起来如下所示,但是从您的代码中,我想您拥有1994年至2013年期间的年度数据

year,population,violentCrime,violentCrimeRate,murder,murderRate,rape,rapeRate,robbery,robberyRate,aggravatedAssault,aggravatedAssaultRate,propertyCrime,propertyCrimeRate,burglary,burglaryRate,larcenyTheft,larcenyTheftRate,motorVehicleTheft,motorVehicleTheftRate
2001,2000,20,0.01,30,0.001,25,0.012,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01
2002,20100,20,0.01,30,0.001,25,0.012,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01
2003,20500,20,0.01,30,0.001,25,0.012,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01
2004,21050,20,0.01,30,0.001,25,0.012,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01

CrimeDataClass.java

package crimedata;

public class CrimeDataClass {

    // Variable for the class
    private int year = 1994;
    private int population = 0;
    private int violentCrime = 0;
    private double violentCrimeRate = 0.0;
    private int murder = 0;
    private double murderRate = 0.0;
    private int rape = 0;
    private double rapeRate = 0.0;
    private int robbery = 0;
    private double robberyRate = 0.0;
    private int aggravatedAssault = 0;
    private double aggravatedAssaultRate = 0.0;
    private int propertyCrime = 0;
    private double propertyCrimeRate = 0.0;
    private int burglary = 0;
    private double burglaryRate = 0.0;
    private int larcenyTheft = 0;
    private double larcenyTheftRate = 0.0;
    private int motorVehicleTheft = 0;
    private double motorVehicleTheftRate = 0.0;

    // Default constructor
    public CrimeDataClass() {
    } // End Constructor

    // Full Parameterize Constructor
    public CrimeDataClass(int year, int population, int violentCrime, double violentCrimeRate, int murder,
            double murderRate, int rape, double rapeRate, int robbery, double robberyRate, int aggravatedAssault,
            double aggravatedAssaultRate, int propertyCrime, double propertyCrimeRate, int burglary,
            double burglaryRate, int larcenyTheft, double larcenyTheftRate, int motorVehicleTheft,
            double motorVehicleTheftRate) {
        this.year = year;
        this.population = population;
        this.violentCrime = violentCrime;
        this.violentCrimeRate = violentCrimeRate;
        this.murder = murder;
        this.murderRate = murderRate;
        this.rape = rape;
        this.rapeRate = rapeRate;
        this.robbery = robbery;
        this.robberyRate = robberyRate;
        this.aggravatedAssault = aggravatedAssault;
        this.aggravatedAssaultRate = aggravatedAssaultRate;
        this.propertyCrime = propertyCrime;
        this.propertyCrimeRate = propertyCrimeRate;
        this.burglary = burglary;
        this.burglaryRate = burglaryRate;
        this.larcenyTheft = larcenyTheft;
        this.larcenyTheftRate = larcenyTheftRate;
        this.motorVehicleTheft = motorVehicleTheft;
        this.motorVehicleTheftRate = motorVehicleTheftRate;

    } // End Constructor

    // Getter Method
    public int getYear() {
        return this.year;
    }

    public int getPopulation() {
        return this.population;
    }

    public int getViolentCrime() {
        return this.violentCrime;
    }

    public double getViolentCrimeRate() {
        return this.violentCrimeRate;
    }

    public int getMurder() {
        return this.murder;
    }

    public double getMurderRate() {
        return this.murderRate;
    }

    public int getRape() {
        return this.rape;
    }

    public double getRapeRate() {
        return this.rapeRate;
    }

    public int getRobbery() {
        return this.robbery;
    }

    public double getRobberyRate() {
        return this.robberyRate;
    }

    public int getAggravatedAssault() {
        return this.aggravatedAssault;
    }

    public double getAggravatedAssaultRate() {
        return this.aggravatedAssaultRate;
    }

    public int getPropertyCrime() {
        return this.propertyCrime;
    }

    public double getPropertyCrimeRate() {
        return this.propertyCrimeRate;
    }

    public int getBurglary() {
        return this.burglary;
    }

    public double getBurglaryRate() {
        return this.burglaryRate;
    }

    public int getLarcenyTheft() {
        return this.larcenyTheft;
    }

    public double getLarcenyTheftRate() {
        return this.larcenyTheftRate;
    }

    public int getMotorVehicleTheft() {
        return this.motorVehicleTheft;
    }

    public double getMotorVehicleTheftRate() {
        return this.motorVehicleTheftRate;
    } // End Getter Method

    // Setter Method
    // Objects are unchangeable due to using values from CSV

    // toString Method
    public String toString() {
        return String.format(
                "\nYear: %d" + "\nPopulation: %d" + "\nViolent Crime: %d" + "\nViolent Crime Rate: %.2f"
                        + "\nMurder: %d" + "\nMurder Rate: %.2f" + "\nRape: %d" + "\nRape Rate: %.2f" + "\nRobbery: %d"
                        + "\nRobbery Rate: %.2f" + "\nAggravated Assault: %d" + "\nAggravated Assault Rate: %.2f"
                        + "\nProperty Crime: %d" + "\nProperty Crime Rate: %.2f" + "\nBurglary: %d"
                        + "\nBurglary Rate: %.2f" + "\nLarceny-Theft: %d" + "\nLarceny-Theft Rate: %.2f"
                        + "\nMotor Vehicle Theft: %d" + "\nMotor Vehicle Theft Rate: %.2f",
                this.year, this.population, this.violentCrime, this.violentCrimeRate, this.murder, this.murderRate,
                this.rape, this.rapeRate, this.robbery, this.robberyRate, this.aggravatedAssault,
                this.aggravatedAssaultRate, this.propertyCrime, this.propertyCrimeRate, this.burglary,
                this.burglaryRate, this.larcenyTheft, this.larcenyTheftRate, this.motorVehicleTheft,
                this.motorVehicleTheftRate);

    } // End toString Method

} // End Class

CrimeDataArray.java

package crimedata;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

public class CrimeDataArray {

    private CrimeDataClass[] crimes;
    private boolean isFileRead;
    private String fileLocation;


    public CrimeDataArray() {
        this.fileLocation = "<add-the-full-path-to-Crimes.csv>";
    }

    public CrimeDataArray(String fileLocation) {
        this.fileLocation = fileLocation;
    }

    public void readFile() {
        try {
            this.crimes =
            Files.readAllLines(Paths.get(fileLocation))
            .stream().skip(1).map(line -> line.split(","))
            .map(tokens -> new CrimeDataClass(
                    Integer.parseInt(tokens[0]),
                    Integer.parseInt(tokens[1]),
                    Integer.parseInt(tokens[2]),
                    Double.parseDouble(tokens[3]),                  
                    Integer.parseInt(tokens[4]),
                    Double.parseDouble(tokens[5]),
                    Integer.parseInt(tokens[6]),
                    Double.parseDouble(tokens[7]),
                    Integer.parseInt(tokens[8]),
                    Double.parseDouble(tokens[9]),
                    Integer.parseInt(tokens[10]),
                    Double.parseDouble(tokens[11]),
                    Integer.parseInt(tokens[12]),
                    Double.parseDouble(tokens[13]),
                    Integer.parseInt(tokens[14]),
                    Double.parseDouble(tokens[15]),
                    Integer.parseInt(tokens[16]),
                    Double.parseDouble(tokens[17]),
                    Integer.parseInt(tokens[18]),
                    Double.parseDouble(tokens[19])      
                    ))
                  .collect(Collectors.toList()).toArray(new CrimeDataClass[0])
             ;
            isFileRead = true;
        } catch (IOException e) {
            throw new RuntimeException("CSV file read failed, error: "+e.getMessage(), e);
        }
    }

    public int countRows(String dataFile) {
        if(!isFileRead) {
            throw new RuntimeException("Data not loaded yet, call readFile() first");
        }// you can use this in all methods if you wish a safety net against NullPointerException
        return crimes.length;   

    } 

    // getPopulationGrowth
    public void getPopulationGrowth() {
        /* Gro: not changed */
        for (int i = 0; i < (crimes.length - 2); i++) {
            System.out.printf("\n%d - %d: %.4f%%", crimes[i].getYear(), crimes[i + 1].getYear(),
                    ((crimes[i + 1].getPopulation() - crimes[i].getPopulation()) / (float) crimes[i].getPopulation())
                            * 100);
        } // End For
    } // End getPopulationGrowth

    // getMaxViolentCrimeRate
    public int getMaxViolentCrimeRate() {
        return Arrays.stream(crimes)
                .max(Comparator.comparing( CrimeDataClass::getViolentCrimeRate))
                .get().getYear();
    } // End getMaxViolentCrimeRate

    // getMinViolentCrimeRate
    public int getMinViolentCrimeRate() {
        return Arrays.stream(crimes)
                .min(Comparator.comparing( CrimeDataClass::getViolentCrimeRate))
                .get().getYear();
    } // End getMinViolentCrimeRate

    // getMaxMurderRate
    public int getMaxMurderRate() {
        return Arrays.stream(crimes)
                .max(Comparator.comparing( CrimeDataClass::getMurderRate))
                .get().getYear();
    } // End getMaxMurderRate

    // getMinMurderRate
    public int getMinMurderRate() {
        return Arrays.stream(crimes)
                .min(Comparator.comparing( CrimeDataClass::getMurderRate))
                .get().getYear();
    } // End getMinMurderRate

    // getMaxRapeRate
    public int getMaxRapeRate() {
        return Arrays.stream(crimes)
                .max(Comparator.comparing( CrimeDataClass::getRapeRate))
                .get().getYear();
    } // End getMaxRapeRate

    // getMinRapeRate
    public int getMinRapeRate() {
        return Arrays.stream(crimes)
                .min(Comparator.comparing( CrimeDataClass::getRapeRate))
                .get().getYear();
    } // End getMinRapeRate

    // getMaxRobberyRate
    public int getMaxRobberyRate() {
        return Arrays.stream(crimes)
                .max(Comparator.comparing( CrimeDataClass::getRobberyRate))
                .get().getYear();
    } // End getMaxRobberyRate

    // getMinRobberyRate
    public int getMinRobberyRate() {
        return Arrays.stream(crimes)
                .min(Comparator.comparing( CrimeDataClass::getRobberyRate))
                .get().getYear();
    } // End getMinRobberyRate

    // getMaxAggravatedAssaultRate
    public int getMaxAggravatedAssaultRate() {
        return Arrays.stream(crimes)
                .max(Comparator.comparing( CrimeDataClass::getAggravatedAssault))
                .get().getYear();
    } // End getMaxAggravatedAssaultRate

    // getMinAggravatedAssaultRate
    public int getMinAggravatedAssaultRate() {
        return Arrays.stream(crimes)
                .min(Comparator.comparing( CrimeDataClass::getAggravatedAssault))
                .get().getYear();
    } // End getMinAggravatedAssaultRate

    // getMaxPropertyCrimeRate
    public int getMaxPropertyCrimeRate() {
        return Arrays.stream(crimes)
                .max(Comparator.comparing( CrimeDataClass::getPropertyCrimeRate))
                .get().getYear();
    } // End getMaxPropertyCrimeRate

    // getMinPropertyCrimeRate
    public int getMinPropertyCrimeRate() {
        return Arrays.stream(crimes)
                .min(Comparator.comparing( CrimeDataClass::getPropertyCrimeRate))
                .get().getYear();
    } // End getMinPropertyCrimeRate

    // getMaxBurglaryRate
    public int getMaxBurglaryRate() {
        return Arrays.stream(crimes)
                .max(Comparator.comparing( CrimeDataClass::getBurglaryRate))
                .get().getYear();
    } // End getMaxBurglaryRate

    // getMinBurglaryRate
    public int getMinBurglaryRate() {
        return Arrays.stream(crimes)
                .min(Comparator.comparing( CrimeDataClass::getBurglaryRate))
                .get().getYear();
    } // End getMinBurglaryRate

    // getMaxLarcenyTheftRate
    public int getMaxLarcenyTheftRate() {
        return Arrays.stream(crimes)
                .max(Comparator.comparing( CrimeDataClass::getLarcenyTheftRate))
                .get().getYear();
    } // End getMaxLarcenyTheftRate

    // getMinLarcenyTheftRate
    public int getMinLarcenyTheftRate() {
        return Arrays.stream(crimes)
                .min(Comparator.comparing( CrimeDataClass::getLarcenyTheftRate))
                .get().getYear();
    } // End getMinLarcenyTheftRate

    // getMaxMotorVehicleTheftRate
    public int getMaxMotorVehicleTheftRate() {
        return Arrays.stream(crimes)
                .max(Comparator.comparing( CrimeDataClass::getMotorVehicleTheftRate))
                .get().getYear();
    } 

    public int getMinMotorVehicleTheftRate() {
        return Arrays.stream(crimes)
                .min(Comparator.comparing( CrimeDataClass::getMotorVehicleTheftRate))
                .get().getYear();
    } 

    // toString
    public String toString() {
        return Arrays.toString(this.crimes);
    }

    public static void main(String[] args) {
        CrimeDataArray array = new CrimeDataArray();
        array.readFile();
        for(CrimeDataClass crime: array.crimes) {
            System.out.println(crime);
        }
    }

} // End Class

TestCrimeData.java

package crimedata;

import java.time.Duration;
import java.time.Instant;
import java.util.Scanner;

public class TestCrimeData {

    public static void main(String[] args) {
        // Start time
        Instant start = Instant.now();

        CrimeDataArray myCrimes = null;
        if(args != null && args.length > 0) {
            myCrimes = new CrimeDataArray(args[0]);
        }else {
            myCrimes = new CrimeDataArray();
        }
        myCrimes.readFile();
        menu(myCrimes);
        // End time
        Instant end = Instant.now();
        System.out.println("Elapsed time (seconds): " + Duration.between(start, end).toNanos() / 1_000_000_000.0);

    } // End Main

    private static void menu(CrimeDataArray crimes) {

        // Interaction with data
        Scanner scannerIn = new Scanner(System.in);

        System.out.println("********** Welcome to the US Crime Statistical Application **************************");
        while (true) {
            System.out.println("");
            System.out.println("\nEnter the number of the question you want answered. Enter ‘Q’ to quit the program.\n"
                    + "\n1. What were the percentages in population growth for each consecutive year from 1994 – 2013?"
                    + "\n2. What year had the highest Violent crime rate?"
                    + "\n3. What year had the lowest Violent crime rate?"
                    + "\n4. What year had the highest Murder rate?" + "\n5. What year had the lowest Murder rate?"
                    + "\n6. What year had the highest Robbery rate?" + "\n7. What year had  the lowest Robbery rate?"
                    + "\n8. What year had the highest Rape rate?" + "\n9. What year had  the lowest Rape rate?"
                    + "\n10. What year had the highest Aggravated Assault rate?"
                    + "\n11. What year had  the lowest Aggravated Assault rate?"
                    + "\n12. What year had the highest Property crime rate?"
                    + "\n13. What year had  the lowest Property crime rate?"
                    + "\n14. What year had the highest Burglary rate?"
                    + "\n15. What year had  the lowest Burglary rate?"
                    + "\n16. What year had the highest Larceny theft rate?"
                    + "\n17. What year had  the lowest Larceny theft rate?"
                    + "\n18. What year had the highest Motor Vehicle theft rate?"
                    + "\n19. What year had  the lowest Motor Vehicle theft rate?"
                    + "\n20. What are the total crimes commited for each year from 1994 – 2013?" + "\nQ. Quit program."
                    + "\nEnter your selection:");

            // Read next line
            String selection = scannerIn.nextLine().trim().toUpperCase();
            if (selection.equalsIgnoreCase("Q")) {
                System.out.println("Thank you for trying the US Crimes Statistics Program.");
                // End loop when Q is entered
                break;
            } // End If
            else {
                switch (selection) {
                case "1":
                    crimes.getPopulationGrowth();
                    break;
                case "2":
                    System.out.printf("The Violent Crime rate was highest in %d", crimes.getMaxViolentCrimeRate());
                    break;
                case "3":
                    System.out.printf("The Violent Crime rate was lowest in %d", crimes.getMinViolentCrimeRate());
                    break;
                case "4":
                    System.out.printf("The Murder rate was highest in %d", crimes.getMaxMurderRate());
                    break;
                case "5":
                    System.out.printf("The Murder rate was lowest in %d", crimes.getMinMurderRate());
                    break;
                case "6":
                    System.out.printf("The Rape rate was highest in %d", crimes.getMaxRapeRate());
                    break;
                case "7":
                    System.out.printf("The Rape rate was lowest in %d", crimes.getMinRapeRate());
                    break;
                case "8":
                    System.out.printf("The Robbery rate was highest in %d", crimes.getMaxRobberyRate());
                    break;
                case "9":
                    System.out.printf("The Robbery rate was lowest in %d", crimes.getMinRobberyRate());
                    break;
                case "10":
                    System.out.printf("The Aggravated Assault rate was highest in %d",
                            crimes.getMaxAggravatedAssaultRate());
                    break;
                case "11":
                    System.out.printf("The Aggravated Assault rate was lowest in %d",
                            crimes.getMinAggravatedAssaultRate());
                    break;
                case "12":
                    System.out.printf("The Aggravated Assault rate was highest in %d",
                            crimes.getMaxPropertyCrimeRate());
                    break;
                case "13":
                    System.out.printf("The Aggravated Assault rate was lowest in %d", crimes.getMinPropertyCrimeRate());
                    break;
                case "14":
                    System.out.printf("The Burglary rate was highest in %d", crimes.getMaxBurglaryRate());
                    break;
                case "15":
                    System.out.printf("The Burglary rate was lowest in %d", crimes.getMinBurglaryRate());
                    break;
                case "16":
                    System.out.printf("The Larceny theft was rate was highest in %d", crimes.getMaxLarcenyTheftRate());
                    break;
                case "17":
                    System.out.printf("The Larceny theft was lowest in %d", crimes.getMinLarcenyTheftRate());
                    break;
                case "18":
                    System.out.printf("The Motor Vehicle theft was rate was highest in %d",
                            crimes.getMaxMotorVehicleTheftRate());
                    break;
                case "19":
                    System.out.printf("The Motor Vehicle theft was lowest in %d", crimes.getMinMotorVehicleTheftRate());
                case "20":
                    System.out.println(crimes.toString());
                    break;
                default:
                    System.out.println("Invalid selection. Please try again.");
                    break;

                } // End Switch

            } // End Else

        } // End While

        scannerIn.close();

    } // End Menu

} // End Class

答案 1 :(得分:-1)

只需替换行

即可解决编译问题
CrimeDataClass myCrimes = new CrimeDataClass();

CrimeDataArray myCrimes = new CrimeDataArray();

由于您没有对要实现的目标进行太多解释,因此很难说您是否会实现自己的目标。我看到您在CrimeDataArray中具有readFile()函数。在将对象传递给菜单方法 menu(crimes) 之前,您不应该调用它来填充 myCrimes 吗?