Hanna Peng Hanna Peng - 23 days ago 6
Java Question

Read a file with a mix of names and numbers and put them into different arrays

I'm in a java introduction class and I know this question might be really simple to you guys, but I do need help.
So I'm trying to read from a file in which contains names and income, and I would like to read them into different arrays.

import java.io.*;
import java.util.Scanner;
public class caltax
{
public static void main (String[] args)
{
String lname[] = new String [12];
String fname[] = new String [12];
Double income[] = new Double [12];
double sincome=0.0;
int i, count=1;

try
{
Scanner infile = new Scanner (new FileInputStream("family.dat"));

while (infile.hasNextLine())
{

lname[12] = infile.next();
fname[12] = infile.next();
income[12] = infile.nextDouble();

}//while

infile.close();

}//try
catch (Exception e)
{
System.out.println("Check the file");
}//catch block



for(i=0;i<12;i++)
{
if (lname[i].equals(lname[i+1]))
{
count++;
sincome += income[i];

//object t is created and constrctor is called
taxcalculation t = new taxcalculation (count, lname, fname, sincome);
t.taxowned();
}//if

}//for
}//main
}//class

Answer

You are almost there. I guess it is just typo lname[12], fname[12],income[12] in that while:

while (infile.hasNextLine())
{
 lname[12] = infile.next();
 fname[12] = infile.next();
 income[12] = infile.nextDouble();

}//while

Assuming that you need only first 12 lines from the file, it must look like:

int lineIdx =0;
while (infile.hasNextLine() && lineIdx < 12)
{
 lname[lineIdx] = infile.next();
 fname[lineIdx] = infile.next();
 income[lineIdx] = infile.nextDouble();
 lineIdx++;
}//while

Updated: working logic (please read through comments in code)... sorry for delay.

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

    // 2. Read file:
    while (infile.hasNextLine()) {
        String personLastName = infile.next();
        // skip 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])) {
                    // got it! 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;
            }
        }
    }// while
    // Print results
    System.out.println("Found " + families.length + " families and their taxes");       
    for (int i=0;i < families.length; i++)
    {
        System.out.println("family " + families[i] + " owes $" + taxes[i]);
    }