我在解析的文本文件数据上执行计算时遇到问题。该程序的目标是从文本创建一个Arraylist,将其发送到类对象,预先形成计算,然后将其打印到表中。我已经正确完成了表格,但是我的小组"类别摘要"并且平均值,最小值和最大值似乎只读取文本中的第一个值。我该怎么办?
主要方法代码
//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());
}
对象代码
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);
}
答案 0 :(得分:2)
你的代码非常复杂。要做你想做的事,你需要创建另一个类来生成统计数据而不是使用相同的HSelectorV2。这是一个简单的解决方案,我已经删除了所有不需要的东西:
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());
}