My question heading might be typed out wrong.. But I'll try to explain it properly here.
I've a class called Sales with some attributes. Here is that class:
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;
}
I need to have a list of sales with those attributes. Now in a different class I've written the following method code:
static Sales salesdata[] = new Sales[50]; // declared in database class OUTside any method
Now here is the troublesome 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;
}
Right now only with this code i am getting the salesdata(0) correctly.. and not the next ones.. How can i fix that?
One more thing: This is just a small method I'm having trouble with. The actual program is a GUI program.. so please excuse the variables assigned and stuff like that, I'll fix it before integrating with the GUI. Just need to make sure that this part works.. :)
Also, i understand that using for(int i=0; i<50; i++) isn't very effective. But I've tried using i<filename.length()
but that only gives the length of 9 it seems.. How can i write a inequality for the length of words in the file?
I would really glad if anyone can give some guild lines on how to fix this code.
Thank you :)