从文本文件进入对象数组

时间:2014-05-06 13:37:06

标签: java arrays class object

我的问题标题可能输入错误..但我会在这里尝试解释它。 我有一个名为Sales的类,有一些属性。这是班级:

protected String salesNo;
protected String customerName;
protected int day ;
protected int month;
protected int year;
protected double totalPrice;

public Sales(String salesNo, String customerName, int day, int month, int year, double totalPrice)
{
    this.salesNo = salesNo;
    this.customerName = customerName;
    this.day = day;
    this.month = month;
    this.year = year;
    this.totalPrice = totalPrice;

}

我需要有一个包含这些属性的销售清单。现在,在另一个课程中,我编写了以下方法代码:

static Sales salesdata[] = new Sales[50]; // declared in database class OUTside any method

现在这是一个麻烦的方法:

public static Sales readToArray(String filename)throws FileNotFoundException, IOException 
{
            //local variables
    String salesNo = null;
    String customerName = null;
    int day = 0;
    int month = 0;
    int year = 0;
    double price = 0;

    Sales sale = new Sales(salesNo, customerName, day, month, year, price);
    String temp[] = new String[100];  //array to hold the file items as a string

    FileReader fileReader = new FileReader(filename);
    BufferedReader in = new BufferedReader(fileReader);

    for(int i=0; i<50; i++) //limited the words in file to exactly 20..
    {
        temp[i] = in.readLine();            //words entered into an array
    }
    System.out.println("First pass");
for(int i=0; i<50; i++) //limited the words in file to exactly 
    {
         System.out.println(temp[i]);

    }

    int count = 0;
    int i = 0;
    if(temp[i] != " ")
    {
        salesNo = temp[i];
        i++;
        customerName = temp[i]; 
        i++;
        day = Integer.parseInt(temp[i]); 
        i++;
        month = Integer.parseInt(temp[i]);
        i++;
        year = Integer.parseInt(temp[i]);
        i++;
        price = Integer.parseInt(temp[i]);  
        i++;
        salesdata[count] = new Sales(salesNo, customerName, day, month, year, price);
        System.out.println(day);
        count++;

    }

    System.out.println("After passing");


    System.out.println("MAINTEST");
    System.out.println(salesdata[0].salesNo);       
System.out.println(salesdata[0].customerName);
System.out.println(salesdata[0].day);
System.out.println(salesdata[0].month);
System.out.println(salesdata[0].year);
System.out.println(salesdata[0].totalPrice);
System.out.println(" ");
System.out.println(salesdata[1].salesNo);       
System.out.println(salesdata[1].customerName);
System.out.println(salesdata[1].day);
System.out.println(salesdata[1].month);
System.out.println(salesdata[1].year);
System.out.println(salesdata[1].totalPrice);
    return sale;
}

现在只有这个代码我才能正确获得salesdata(0)..而不是下一个..我怎么能解决这个问题?

还有一件事:这只是我遇到麻烦的一个小方法。实际的程序是一个GUI程序..所以请原谅分配的变量和类似的东西,我会在与GUI集成之前修复它。只需要确保这部分有效..:)

另外,据我所知,使用for(int i = 0; i <50 ; i ++)并不是非常有效。但是我尝试过使用i<filename.length(),但是只看到9的长度。我怎么能写出文件中单词长度的不等式?

如果有人能就如何修复此代码提供一些公会话,我真的很高兴。  谢谢:))

2 个答案:

答案 0 :(得分:1)

我不知道您的代码还有什么问题,但if条件不在循环中。对于i = 0和count = 0,它只执行一次,这意味着该行也只执行一次:

salesdata[count] = new Sales(salesNo, customerName, day, month, year, price);

答案 1 :(得分:1)

您的代码存在一些问题。如果我理解正确,我会做类似的事情:

BufferedReader in = new BufferedReader(in);
Scanner scan = new Scanner(in);

int words=0;
int count=0;
while (scan.hasNext()) {
    salesdata[count++] = new Sales(scan.next(), scan.next(), scan.nextInt(), can.nextInt(), scan.nextInt(), scan.nextDouble());         
    //if ((words+=6)>=50) break;
}

如果您想将输入限制为50个字,只需取消注释if

即可