Hanna Peng Hanna Peng - 7 days ago 5
Java Question

How to count different arrays line numbers from reading a file

I'm doing a school assignment. It requires me to read names and income from a file and put everyone that is in the same family together to calculate the family's tax.
I have already put people into different arrays, but I am having a hard time to count how many family members that each family has.

The file that I am reading from looks like this:


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


I have tried to insert count++ into several places but none of them would give me the correct answer. Please help me here.

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]);

}
}
}

Answer

just add another array

...
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
Comments