读取包含名称和数字混合的文件并将它们放入不同的数组中
Read a file with a mix of names and numbers and put them into different arrays
我正在做 java 介绍 class,我知道这个问题对您来说可能很简单,但我确实需要帮助。
所以我试图从一个包含姓名和收入的文件中读取,我想将它们读入不同的数组。
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
为什么要把它们读入不同的数组?这是你的作业要求的吗?一个更好的解决方案是使用您的属性创建一个对象,例如 Employee,然后将每个 Employee 添加到 List<Employee>
如果您询问如何将项目迭代添加到数组中,您需要一个从 0 开始到 array.length - 1 结束的索引。
你快到了。我想这只是拼写错误 lname[12], fname[12],income[12]在那段时间里:
while (infile.hasNextLine())
{
lname[12] = infile.next();
fname[12] = infile.next();
income[12] = infile.nextDouble();
}//同时
假设您只需要文件的前 12 行,它必须如下所示:
int lineIdx =0;
while (infile.hasNextLine() && lineIdx < 12)
{
lname[lineIdx] = infile.next();
fname[lineIdx] = infile.next();
income[lineIdx] = infile.nextDouble();
lineIdx++;
}//while
已更新:工作逻辑(请阅读代码中的注释)...抱歉延迟。
// 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]);
}
我正在做 java 介绍 class,我知道这个问题对您来说可能很简单,但我确实需要帮助。 所以我试图从一个包含姓名和收入的文件中读取,我想将它们读入不同的数组。
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
为什么要把它们读入不同的数组?这是你的作业要求的吗?一个更好的解决方案是使用您的属性创建一个对象,例如 Employee,然后将每个 Employee 添加到 List<Employee>
如果您询问如何将项目迭代添加到数组中,您需要一个从 0 开始到 array.length - 1 结束的索引。
你快到了。我想这只是拼写错误 lname[12], fname[12],income[12]在那段时间里:
while (infile.hasNextLine())
{
lname[12] = infile.next();
fname[12] = infile.next();
income[12] = infile.nextDouble();
}//同时
假设您只需要文件的前 12 行,它必须如下所示:
int lineIdx =0;
while (infile.hasNextLine() && lineIdx < 12)
{
lname[lineIdx] = infile.next();
fname[lineIdx] = infile.next();
income[lineIdx] = infile.nextDouble();
lineIdx++;
}//while
已更新:工作逻辑(请阅读代码中的注释)...抱歉延迟。
// 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]);
}