首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从读取文件中计数不同的数组行号

如何从读取文件中计数不同的数组行号
EN

Stack Overflow用户
提问于 2016-11-26 18:20:26
回答 2查看 75关注 0票数 0

我在做学校作业。它要求我从一个文件中读取姓名和收入,并将同一家庭中的每一个人放在一起计算家庭税。我已经把人分成了不同的数组,但我很难计算出每个家庭有多少家庭成员。

我正在读取的文件如下所示:

琼斯·拉尔夫39765.45 琼斯玛丽18532.00 琼斯·弗兰克·0.00 汉森约翰63321.00 汉森琳达8765.00 墨菲杰夫53786.75 墨菲尼娜65432.00 墨菲·艾伦·0.00 墨菲·大卫·0.00 西蒙麦克7654.00 西蒙珍妮特44762.00 西蒙·汤姆·0.00

我试着把count++插入到几个地方,但是没有一个地方能给我正确的答案。请帮帮我。

代码语言:javascript
复制
import java.io.*;
import java.util.Scanner;
public class caltax
{
public static void main (String[] args) 
{    

// 1. Define two arrays
String[] families = null;
double[] taxes = null;

// 2. Read file:
 try 
  {
  Scanner infile = new Scanner (new FileInputStream("family.dat"));

while (infile.hasNextLine()) 
{
    String personLastName = infile.next();

    //skip the first name 
    infile.next();

    double personTax = infile.nextDouble();

    // add person data
    if (null == families) 
    {
        // create array for
        families = new String[] { personLastName };
        taxes = new double[] { personTax };
    } 
    else 
    {
        boolean familyExists = false;

        // check existing families
        for (int i = 0; i < families.length; i++) 
        {
            if (personLastName.equals(families[i])) 
            {
                // add personTax to family owed taxes
              taxes[i] += personTax;  

                familyExists = true;
                break;
            }
        }
        if (!familyExists) 
        {
            // Extend arrays to put new family
            // create temp arrays with size+1

            String[] tmpFamilies = new String[families.length + 1];
            double[] tmpTaxes = new double[taxes.length + 1];


            System.arraycopy(families, 0, tmpFamilies, 0, families.length);
            System.arraycopy(taxes, 0, tmpTaxes, 0, taxes.length);

            // set new last elements data
            tmpFamilies[tmpFamilies.length - 1] = personLastName;

            tmpTaxes[tmpTaxes.length - 1] = personTax;

            // replace families and taxes with newly created tmp arrays
            families = tmpFamilies;
            taxes = tmpTaxes;
        }
    }//else

}// while


infile.close();
 }//try

 catch(Exception e) 
{
System.out.println(e.toString());
}//catch block 

// Print results

for (int i=0;i < families.length; i++)
{
    System.out.println("family " + families[i] + " owes $" + taxes[i]);

}
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-27 06:38:24

只需添加另一个数组

代码语言:javascript
复制
...
int []familyCount = null
if (null == families) 
{
    // create array for
    families = new String[] { personLastName };
    taxes = new double[] { personTax };
    familyCount = new int[] {1};

} 
else 
{
    boolean familyExists = false;

    // check existing families
    for (int i = 0; i < families.length; i++) 
    {
        if (personLastName.equals(families[i])) 
        {
            // add personTax to family owed taxes
          taxes[i] += personTax;  

          familyCount[i] ++;

            familyExists = true;
            break;
        }
    }
    if (!familyExists) 
    {
        // Extend arrays to put new family
        // create temp arrays with size+1

        String[] tmpFamilies = new String[families.length + 1];
        double[] tmpTaxes = new double[taxes.length + 1];
        int[] tmpFamilyCount = new int[familyCount.length+1];


        ...
        System.arraycopy(familyCount, 0, tmpFamilyCount, 0, familyCount.length);

        // set new last elements data 
        ...
        tmpFamilyCount[tmpFamilyCount.length-1] = 1;

        // replace families and taxes with newly created tmp arrays 
        ...
        familyCount = tmpFamilyCount;
    }
}//else
票数 0
EN

Stack Overflow用户

发布于 2016-11-26 19:31:10

代码语言:javascript
复制
// 1. Define two arrays
...
HashMap<String,Integer> familyMember=null;

    if (null == families) 
    {
        ...
        familyMember = new HashMap();
        familyMember.put(personLastName, new Integer(1));        
    } else {

      ....
       for (int i = 0; i < families.length; i++) 
        {
            if (personLastName.equals(families[i])) 
            {

              int totalPerson = familyMember.get(personLastName).intValue();   
              familyMember.put(personLastName, new Integer(totalPerson));
                .....
            }
        }

       if (!familyExists) 
        {
              ....
              familyMember.put(personLastName, new Integer(1));
        }
    }

  // Print Results

  for (String key : familyMember.keySet()) {
    System.out.println("family " + key + " has " + familyMember.get(key).toString() + " person");
  }

更新-没有HashMap

用有效的拆分方法

代码语言:javascript
复制
// 1. Define two arrays
...
String[] familyMember=null;

    if (null == families) 
    {
        ...
        familyMember = new String[]{personLastName +"-1"};        
    } else {

      ....
       for (int i = 0; i < families.length; i++) 
        {
            if (personLastName.equals(families[i])) 
            {

              int totalPerson = Integer.parseInt(familyMember[i].split("-")[1]);   
              familyMember[i] = families[i]+"-"+String.valueOf(totalPerson+1);
                .....
            }
        }

       if (!familyExists) 
        {
              ....
              String[] tmpFamilyMember = new String[familyMember.length + 1];
             System.arraycopy(familyMember, 0, tmpFamilyMember, 0, familyMember.length);

              tmpFamilyMember[tmpFamilyMember.length - 1] = personLastName+"-1";

           /* **UPDATE** */
           familyMember =  tmpFamilyMember;

        }
    }

  // Print Results

  for (String fam : familyMember) {
    String[] familyWithCount = fam.split("-");
    System.out.println("family " + familyWithCount[0] + " has " + familyWithCount[1] + " person");
  }

结果:

琼斯家族欠58297.45美元

汉森家族欠72086.0美元

墨菲家族欠119218.75美元

西蒙家族欠52416.0美元

琼斯家族有3个人

汉森家有两个人

墨菲家族有4个人

西蒙家有3个人

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40821571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档