简体   繁体   中英

Java Arraylist calculations reading only first line from text-file

I am having issues with calculations performed on parsed text file data. The goal of the program is to create an Arraylist from text, send it to a class object, preform calculations, then print it to a table. I have the table completed correctly, but my group "summary of categories" and average, minimum and maximum seem to read only the first value from text. What can I do about this?

Main Method code

        //Variable Initiallization
    int year, pressure, wind;
    String month, name;
    //Reading Data from file/create ArrayList
    Scanner inFile = new Scanner(new File("hurricanedata.txt"));
    ArrayList<HSelectorV2> datapool = new ArrayList<HSelectorV2>();
    while(inFile.hasNextLine()){
        datapool.add(new HSelectorV2(Integer.parseInt(inFile.next()),
                                     inFile.next(),
                                     Integer.parseInt(inFile.next()),
                                     Integer.parseInt(inFile.next()),
                                     inFile.next()));

    }
    inFile.close();
    //get arrayList size
    int size = datapool.size();
    //getting max and mins
    int catMin = Integer.MAX_VALUE;
    int catMax = Integer.MIN_VALUE;
    int pressureMin = Integer.MAX_VALUE;
    int pressureMax = Integer.MIN_VALUE;
    double windMin = Double.MAX_VALUE;
    double windMax = Double.MIN_VALUE;
    //maximums
    for (HSelectorV2 dataRecord : datapool){
        if (dataRecord.getPressure() > pressureMax)
            pressureMax = dataRecord.getPressure();
    }
    for (HSelectorV2 dataRecord : datapool){
        if (dataRecord.getWindMPH() > windMax)
            windMax = dataRecord.getWindMPH();
    }
    for (HSelectorV2 dataRecord : datapool){
        if (dataRecord.getCategory() > catMax)
            catMax = dataRecord.getCategory();
    }
    //minimums
    for (HSelectorV2 dataRecord : datapool){
        if (dataRecord.getPressure() < pressureMin)
            pressureMin = dataRecord.getPressure();
    }
    for (HSelectorV2 dataRecord : datapool){
        if (dataRecord.getWindMPH() < windMin)
            windMin = dataRecord.getWindMPH();
    }
    for (HSelectorV2 dataRecord : datapool){
        if (dataRecord.getCategory() < catMin)
            catMin = dataRecord.getCategory();
    }
    for(HSelectorV2 dataRecord : datapool){
        dataRecord.calcCategory();
        dataRecord.getCategory();
        dataRecord.getPressure();
        dataRecord.getWindMPH();
    }
    HSelectorV2 dataRecord;
    //Output
    System.out.println("\t\t\tHurricanes 1995 - 2015\n");
    System.out.println(" Year\tHurricane\tCategory\tPressure (mb)\tWind Speed (mph)");
    System.out.println("=========================================================================");
    for(int index = 0; index < datapool.size(); index++){
        System.out.println(datapool.get(index));
    }
    System.out.println("=========================================================================");
    for(int i = 0; i < 1; i++){
        dataRecord = datapool.get(i);
        System.out.printf("%9s%20.2f%16.2f%18.2f\n" , "Average" , dataRecord.getcatAvg(), dataRecord.getpressureAvg() , dataRecord.getwindAvg());//average
        System.out.printf("%9s%20d%16d%18.2f\n" , "Maximum" , catMax , pressureMax , windMax);//max
        System.out.printf("%9s%20d%16d%18.2f\n" , "Minimum" , catMin , pressureMin , windMin);//min
        System.out.println();
        System.out.println("Summary of Categories:");
        System.out.println("\tCat 1: " + dataRecord.getCat1());
        System.out.println("\tCat 2: " + dataRecord.getCat2());
        System.out.println("\tCat 3: " + dataRecord.getCat3());
        System.out.println("\tCat 4: " + dataRecord.getCat4());
        System.out.println("\tCat 5: " + dataRecord.getCat5());
    }

Object Code

    private int myYear, myWindkts, myPressure, category, cat1, cat2, cat3, cat4, cat5;
private String myMonth, myName;
private double myWindMPH, windAvg, pressureAvg, catAvg;
public HSelectorV2(int year, String month, int pressure, int wind, String name)
{
    myYear = year;
    myMonth = month;
    myPressure = pressure;
    myWindkts = wind;
    myName = name;
}
//Mutator Method to Calculate WindMPH, create average totals, and calculate category
public void calcCategory(){
        myWindMPH = (1.15078) * myWindkts;
        windAvg += myWindMPH;
        pressureAvg += myPressure;
        //category determining
        if(myWindMPH > 74 && myWindMPH < 95)
        {
            category = 1;
            catAvg += category;
            cat1++;
        }
        else if(myWindMPH > 96 && myWindMPH < 110)
        {
            category = 2;
            catAvg += category;
            cat2++;
        }
        else if(myWindMPH > 111 && myWindMPH < 129)
        {
            category = 3;
            catAvg += category;
            cat3++;
        }
        else if(myWindMPH > 130 && myWindMPH < 156)
        {
            category = 4;
            catAvg += category;
            cat4++;
        }
        else if(myWindMPH > 157)
        {
            category = 5;
            catAvg += category;
            cat5++;
        }
}
//Mutator methods for calculating averages
public void catAvg(int size){
    catAvg = catAvg / size;
}
public void windAvg(int size){
    windAvg = windAvg / size;
}
public void pressureAvg(int size){
    pressureAvg = pressureAvg / size;
}
//getter methods
public int getCategory(){
    return category;
}
public int getPressure(){
    return myPressure;
}
public double getWindMPH(){
    return myWindMPH;
}
public double getcatAvg(){
    return catAvg;
}
public double getwindAvg(){
    return windAvg;
}
public double getpressureAvg(){
    return pressureAvg;
}
public int getCat1(){
    return cat1;
}
public int getCat2(){
    return cat2;
}
public int getCat3(){
    return cat3;
}
public int getCat4(){
    return cat4;
}
public int getCat5(){
    return cat5;
}
public String toString(){
    return String.format("%6d%13s%10d%16d%18.2f\n", myYear, 
                                myName , category , myPressure , myWindMPH);
}

Here is a copy of the text file which is being read.

Your code it's very complicated. To do what you want, you need to create another class o generate the statistics and not use the same HSelectorV2. Here is a simple solution, I've removed all that is not needed:

public class HSelectorV2 {
private int myYear, myWindkts, myPressure, category;
private String myMonth, myName;
private double myWindMPH;
public HSelectorV2(int year, String month, int pressure, int wind, String name)
{
    myYear = year;
    myMonth = month;
    myPressure = pressure;
    myWindkts = wind;
    myName = name;
}
//Mutator Method to Calculate WindMPH, create average totals, and calculate category
public void calcCategory(){
    myWindMPH = (1.15078) * myWindkts;
    if(myWindMPH > 74 && myWindMPH < 95)
    {
        category = 1;
    }
    else if(myWindMPH > 96 && myWindMPH < 110)
    {
        category = 2;
    }
    else if(myWindMPH > 111 && myWindMPH < 129)
    {
        category = 3;
    }
    else if(myWindMPH > 130 && myWindMPH < 156)
    {
        category = 4;
    }
    else if(myWindMPH > 157)
    {
        category = 5;
    }
}
//getter methods
public int getCategory(){
    return category;
}
public int getPressure(){
    return myPressure;
}
public double getWindMPH(){
    return myWindMPH;
}
public String toString(){
    return String.format("%6d%13s%10d%16d%18.2f\n", myYear, 
            myName , category , myPressure , myWindMPH);
}
}

    public static void main(String[] args) throws FileNotFoundException {
    //Reading Data from file/create ArrayList
    Scanner inFile = new Scanner(new File("hurricanedata.txt"));
    ArrayList<HSelectorV2> datapool = new ArrayList<HSelectorV2>();
    while(inFile.hasNextLine()){
        datapool.add(new HSelectorV2(Integer.parseInt(inFile.next()),
                inFile.next(),
                Integer.parseInt(inFile.next()),
                Integer.parseInt(inFile.next()),
                inFile.next()));

    }
    inFile.close();
    //getting max and mins
    int catMin = Integer.MAX_VALUE;
    int catMax = Integer.MIN_VALUE;
    int pressureMin = Integer.MAX_VALUE;
    int pressureMax = Integer.MIN_VALUE;
    double windMin = Double.MAX_VALUE;
    double windMax = Double.MIN_VALUE;

    List<Integer> categories = new ArrayList<>();
    List<Integer> pressures = new ArrayList<>();
    List<Double> winds = new ArrayList<>();

    for (HSelectorV2 dataRecord : datapool){
        if (dataRecord.getPressure() > pressureMax)
            pressureMax = dataRecord.getPressure();

        if (dataRecord.getWindMPH() > windMax)
            windMax = dataRecord.getWindMPH();

        if (dataRecord.getCategory() > catMax)
            catMax = dataRecord.getCategory();

        if (dataRecord.getPressure() < pressureMin)
            pressureMin = dataRecord.getPressure();

        if (dataRecord.getWindMPH() < windMin)
            windMin = dataRecord.getWindMPH();

        if (dataRecord.getCategory() < catMin)
            catMin = dataRecord.getCategory();

        dataRecord.calcCategory();

        categories.add(Integer.valueOf(dataRecord.getCategory()));
        pressures.add(Integer.valueOf(dataRecord.getPressure()));
        winds.add(Double.valueOf(dataRecord.getWindMPH()));

    }

    //Output
    System.out.println("\t\t\tHurricanes 1995 - 2015\n");
    System.out.println(" Year\tHurricane\tCategory\tPressure (mb)\tWind Speed (mph)");
    System.out.println("=========================================================================");
    for(int index = 0; index < datapool.size(); index++){
        System.out.println(datapool.get(index));
    }
    System.out.println("=========================================================================");

    IntSummaryStatistics catStats = categories.stream().mapToInt((c) -> c).summaryStatistics();
    IntSummaryStatistics pressureStats = pressures.stream().mapToInt((p) -> p).summaryStatistics();
    DoubleSummaryStatistics windsStats = winds.stream().mapToDouble((w) -> w).summaryStatistics();
    System.out.printf("%9s%20.2f%16.2f%18.2f\n", "Average", catStats.getAverage(), pressureStats.getAverage(), windsStats.getAverage());//average
    System.out.printf("%9s%20d%16d%18.2f\n", "Maximum", catStats.getMax(), pressureStats.getMax() , windsStats.getMax());//max
    System.out.printf("%9s%20d%16d%18.2f\n", "Minimum", catStats.getMin(), pressureStats.getMin() , windsStats.getMin());//min
    System.out.println();
    System.out.println("Summary of Categories:");
    System.out.println("\tCat 1: " + categories.stream().filter(c -> c == 1).count());
    System.out.println("\tCat 2: " + categories.stream().filter(c -> c == 2).count());
    System.out.println("\tCat 3: " + categories.stream().filter(c -> c == 3).count());
    System.out.println("\tCat 4: " + categories.stream().filter(c -> c == 4).count());
    System.out.println("\tCat 5: " + categories.stream().filter(c -> c == 5).count());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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