Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文本文件输入对象数组_Java_Arrays_Class_Object - Fatal编程技术网

Java 从文本文件输入对象数组

Java 从文本文件输入对象数组,java,arrays,class,object,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

我的问题标题可能打错了。。但我会尽量在这里解释清楚。 我有一个叫做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;
}
publicstaticsales readToArray(字符串文件名)抛出FileNotFoundException,IOException
{
//局部变量
字符串salesNo=null;
字符串customerName=null;
整日=0;
整月=0;
整年=0;
双倍价格=0;
销售=新销售(销售编号、客户名称、日、月、年、价格);
String temp[]=新字符串[100];//将文件项作为字符串保存的数组
FileReader FileReader=新的FileReader(文件名);
BufferedReader in=新的BufferedReader(文件读取器);

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

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

您的代码有一些问题。如果我理解正确,我会执行以下操作:

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个单词,只需取消注释
如果

哦,是的!我没有意识到它没有循环。谢谢。但这就是我现在面临的全部考验。如果我循环它,我会得到一个数字格式的异常。这真的很有帮助!非常感谢:)