0

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 :)

2 Answers 2

1

Your code has some problems. If I understand it correctly, I'd do something like:

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;
}

Anf if you want limit the input to 50 words just uncomment the if

Sign up to request clarification or add additional context in comments.

Comments

1

I don't know what else is wrong with your code but the if condition is not in a loop. It's only executed once for i = 0 and count = 0, which means this line is executed only once as well:

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

1 Comment

Oh yea! I didnt realize it wasnt looped. Thanks. But that is the whole ordeal for me right now. If i loop it im getting a NumberFormatException.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.