如何获取ArrayList以打印出不同数据类型的信息?

时间:2015-01-30 23:02:42

标签: java arraylist

我有一项任务,我必须按照“负担得起的医疗保健法”注册的符合条件的公民的最低百分比排列美国各州。我不得不创建一个ArrayList,它拥有两种不同类型的数据:两个字符串和三个双精度数据。我已经接近完成但是我被困在了:

如何逐行打印状态?我想我应该使用t /开始新的一行。

如何按从高到低的百分比顺序打印状态?

到目前为止

代码。

import java.util.*;
import java.io.*;
import java.util.ArrayList;

// Calculates the percentage of citizens enrolled in the Affordable Healthcare Act 
// Lists the states in order of highest percentage of enrolled citizens in the Affordable Healthcare Act

public class ACA {

   public static void main( String[] args ) throws FileNotFoundException {
      Scanner keyboard = new Scanner(System.in); // creates an object of the Scanner class


      System.out.println("Enter filename"); // prompts the user for the name of the file
      String filename = keyboard.next(); // the user response is stored

      File inputFile = new File(filename+".txt"); // takes the filename and creates an object of the File class
      Scanner fileIn = new Scanner(inputFile); // takes the object and converts the file to an obect of the Scanner class to allow it to be read

      // creates an object of the ArrayList class 
      ArrayList<ACAdata> info = new ArrayList<>();


      // variables declared for each column of data in the line
      String state = " "; // State abbreviation
      String org = " "; // Organizer of the ACA marketplace, either FFM for federally facilitated marketplace or SBM for state based marketplace.
      double numEll = 0; // Number of citizens in the state eligible for ACA coverage
      double numEn = 0; // Number of citizens who enrolled in an ACA plan 

      // while loop will evaluate as long as there are rows of data in the text file
      while ( fileIn.hasNext() ) {
         state = fileIn.next(); // individual state      
         org = fileIn.next();  // organization       
         numEll = fileIn.nextDouble(); // number of elligible citizens for that state for the Affordable Healthcare Act       
         numEn = fileIn.nextDouble(); // number of citizens enrolled in the Affordable Healthcare Act


         double percentage = per( numEll, numEn ); // calls the per method to calculate a percentage for each state

         // adds the 5 fields of data to a new ArrayList that holds the information for each state
         info.add(new ACAdata( state, org, numEll, numEn, percentage));


      }

      // Prints out the information about the state
      for ( int i = 0; i < info.size(); i++ ) {
         System.out.println((info.get(i)).toString());
      }


   }

    // method that finds the percentage of enrolled citizens that are elligible for the Affordable Care Act
   public static double per( double Ell, double En ) {
      double calculation = En / Ell * 100;  // divides the Enrolled by the number of elligible citizens 
      return calculation; // returns the calculation
   }
}

public class ACAdata {
 // variables declared for each column of data in the line
   String state = " "; // State abbreviation
   String org = " "; // Organizer of the ACA marketplace, either FFM for federally facilitated marketplace or SBM for state based marketplace.
   double numEll = 0; // Number of citizens in the state eligible for ACA coverage
   double numEn = 0; // Number of citizens who enrolled in an ACA plan
   double percentage = 0;

   public ACAdata ( String state, String org, double numEll, double numEn, double percentage ) {
      state = state;
      org = org;
      numEll = numEll;
      numEn = numEn;
      percentage = percentage;
   }
   public String getState() {
      return this.state;
   }
   public void setState(String state) {
      this.state = state;
   }
   public String getOrg() {
      return this.org;
   }
   public void setOrg( String org ) {
      this.org = org;
   }
   public double getNumEll() {
      return this.numEll;
   }
   public void setNumEll( double numEll ) {
      this.numEll = numEll;
   }
   public double getNumEn( double numEn ) {
      return this.numEn;
   }
   public double getPercentage() {
      return this.percentage;
   }
   public void setPercentage( double percentage ) {
      this.percentage = percentage;
   }
}

2 个答案:

答案 0 :(得分:1)

使ACAdata对象可排序。 如果你有这个对象,你可以对它进行排序,然后用。

输出
ArrayList<ACAdata> list = new ArrayList<>();
        Collections.sort(list);
        for (Iterator<ACAdata> iterator = list.iterator(); iterator.hasNext();) {
            ACAdata next = iterator.next();
            System.out.println(next.toString());
        } 

public class ACAdata implements Comparable<ACAdata> {

    // variables declared for each column of data in the line
    String state = " "; // State abbreviation
    String org = " "; // Organizer of the ACA marketplace, either FFM for federally facilitated marketplace or SBM for state based marketplace.
    double numEll = 0; // Number of citizens in the state eligible for ACA coverage
    double numEn = 0; // Number of citizens who enrolled in an ACA plan
    double percentage = 0;

    public ACAdata(String state, String org, double numEll, double numEn, double percentage) {
        state = state;
        org = org;
        numEll = numEll;
        numEn = numEn;
        percentage = percentage;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getOrg() {
        return this.org;
    }

    public void setOrg(String org) {
        this.org = org;
    }

    public double getNumEll() {
        return this.numEll;
    }

    public void setNumEll(double numEll) {
        this.numEll = numEll;
    }

    public double getNumEn(double numEn) {
        return this.numEn;
    }

    public double getPercentage() {
        return this.percentage;
    }

    public void setPercentage(double percentage) {
        this.percentage = percentage;
    }

    @Override
    public int compareTo(ACAdata o) {
        if (percentage > o.percentage) {
            return -1;
        }
        if (percentage < o.percentage) {
            return 1;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "Put the info you want here!";
    }

}

答案 1 :(得分:0)

查看您的代码,最简单的解决方案(我能想到的)是覆盖toString中的ACAdata,例如(按照您的需要添加和格式化)

@Override
public String toString() {
    return String.format("%s %s %.2f %.2f", state, org, numEll, numEn);
}

然后您可以打印整个ArrayList或单个ACAdata实例。您已经拨打toString()了,但是您不必...当您尝试打印toString()时,Java会隐式地为您拨打Object电话( Object中的默认实现只是没有做你想做的事。)