gpt4 book ai didi

java - 读取包含名称和数字混合的文件并将它们放入不同的数组中

转载 作者:行者123 更新时间:2023-12-01 09:14:46 25 4
gpt4 key购买 nike

我正在参加 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

最佳答案

你就快到了。我猜这只是拼写错误 lname[12], fname[12],venue[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 - 读取包含名称和数字混合的文件并将它们放入不同的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40642634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com