我正在做一份学校作业。它要求我从文件中读取姓名和收入,并将同一家庭中的每个人放在一起计算家庭税。 我已经把人们分成不同的阵列,但我很难计算每个家庭有多少家庭成员。
我正在阅读的文件如下所示:
Jones Ralph 39765.45
Jones Mary 18532.00
Jones Frank 0.00
Hansen John 63321.00
Hansen Linda 8765.00
Murphy Jeff 53786.75
Murphy Nina 65432.00
Murphy Ellen 0.00
Murphy David 0.00
Simon Mike 7654.00
Simon Janet 44762.00
Simon Tom 0.00
我试图将count ++插入几个地方,但没有一个会给我正确答案。请帮帮我。
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]);
}
}
}
答案 0 :(得分:0)
// 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
使用有用的 split 方法
// 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美元家庭Simon欠52416.0美元
家庭琼斯有3个人 家庭汉森有2个人 家庭墨菲有4个人家庭西蒙有3个人
答案 1 :(得分:0)
只需添加另一个数组
...
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