试图读取一个文件并获取同一行上的单独信息



我试图从这个文件中获取每一条单独的信息,将其分配给一个变量,使用数据创建一个Customer对象,然后将其添加到Customer对象数组中。这是针对一个项目的,我必须使用数组,而不是数组列表,并且我必须在readCustomerFile方法中使用2个扫描程序。需要帮助完成读取CustomerFile,不知道如何使用第二台扫描仪

这是文件:

1 10001 Smith Sam 34.5 100.0 63.5 350.0
2 10002 Jones Pat 34.5 100.0 63.5 300.0
3 10003 King Kelly 34.5 100.0 63.5 300.0
4 10004 Jenkins Jordan 34.5 100.0 63.5 300.0 100.0

第一个数字是这个人是什么类型的Customer对象。1是PreferredCustomer,2是SilverPreferredClient,3是GoldPreferredCustomer。这是构造函数:

public class RewardsProcessor {
   private Customer[] customers;
   private String[] invalidRecords;
   public RewardsProcessor() {
      customers = new Customer[0];
      invalidRecords = new String[0];
   }

到目前为止,我的readCustomerFile方法(我需要帮助的方法)应该使用2个扫描仪。一个读取文件,另一个逐行读取信息

 public void readCustomerFile(String fileName) throws IOException {
     Scanner scanFile = new Scanner(new File(fileName)); 
     int category = 0;
     int account = 0;
     String name = " ";
     Double[] purchase = new Double[100];

     while(scanFile.hasNext()) {
         String next = scanFile.nextLine();

这是客户和首选SilverCustomer的参考

public Customer(String acctNumberIn, String nameIn) {
      acctNumber = acctNumberIn;
      name = nameIn;
      purchases = new double[0];
   }

public PreferredSilverCustomer(String acctNumber, String name) {
      super(acctNumber, name);
      category = "Preferred Silver Customer";
   }

回答我对问题的理解程度

步骤1)

 Scanner readFile=new Scanner(new File(filename);
 String tempValue=""; // used for storing value while access data
 String[] contents=new String[100];
 int i=0;
 while(readFile.hasNextLine())
{  
  contents[i]=readFile.nextLine(); // this array contain all your information in file line by line
  i++;
}

步骤2)

现在内容数组包含文件中的所有信息访问单行信息使用

String[] information=contents[0].split(" "); // now inforamtion[0] contain 1,inforamtion[1] contain 10001,.....

最新更新