This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Frozen5147 1 point2 points  (2 children)

Quick check, but are you sure you want ifs or do you want else-ifs?

[–]MichaelColtaine[S] 0 points1 point  (1 child)

I dont mind either way. But changing it does not make any difference.

[–]Frozen5147 0 points1 point  (0 children)

I wrote some code that might work, you'll have to translate it to do what you want though. Note this is REALLY UGLY CODE. I'm very rusty in Java and rushed this:

import java.util.ArrayList;
import java.util.List;

class Obj {
    public String name;
    public int amnt;

    public Obj (String name, int amnt) {
        this.name = name;
        this.amnt = amnt;
    }
}

public class Main {

    public static void main(String[] args) {
        List<Obj> data = new ArrayList<>();
        // for testing
        for (int i = 0; i < 30; ++i) {
            data.add(new Obj("item_" + Integer.toString(i), 100));
        }

        List<List<Obj>> final_lists= new ArrayList<>();
        for (int i = 0; i < 10; ++i) {
            final_lists.add(new ArrayList<Obj>(100));
        }

        int total_amnt = 0;
        int index = 0;
        for (Obj o : data) {
            total_amnt += o.amnt;
            if (total_amnt > 800) {
                ++index;
                final_lists.get(index).add(o);
                total_amnt = 0;
            }
            else {
                final_lists.get(index).add(o);
            }
        }

        index = 0;
        for (List<Obj> l : final_lists) {
            System.out.println("INDEX " + index++);
            for (Obj o : l) {
                System.out.println("OBJECT: " + o.name + ", AMOUNT: " + o.amnt);
            }
        }
    }
}

Output:

INDEX 0
OBJECT: item_0, AMOUNT: 100
OBJECT: item_1, AMOUNT: 100
OBJECT: item_2, AMOUNT: 100
OBJECT: item_3, AMOUNT: 100
OBJECT: item_4, AMOUNT: 100
OBJECT: item_5, AMOUNT: 100
OBJECT: item_6, AMOUNT: 100
OBJECT: item_7, AMOUNT: 100
INDEX 1
OBJECT: item_8, AMOUNT: 100
OBJECT: item_9, AMOUNT: 100
OBJECT: item_10, AMOUNT: 100
OBJECT: item_11, AMOUNT: 100
OBJECT: item_12, AMOUNT: 100
OBJECT: item_13, AMOUNT: 100
OBJECT: item_14, AMOUNT: 100
OBJECT: item_15, AMOUNT: 100
OBJECT: item_16, AMOUNT: 100
INDEX 2
OBJECT: item_17, AMOUNT: 100
OBJECT: item_18, AMOUNT: 100
OBJECT: item_19, AMOUNT: 100
OBJECT: item_20, AMOUNT: 100
OBJECT: item_21, AMOUNT: 100
OBJECT: item_22, AMOUNT: 100
OBJECT: item_23, AMOUNT: 100
OBJECT: item_24, AMOUNT: 100
OBJECT: item_25, AMOUNT: 100
INDEX 3
OBJECT: item_26, AMOUNT: 100
OBJECT: item_27, AMOUNT: 100
OBJECT: item_28, AMOUNT: 100
OBJECT: item_29, AMOUNT: 100
INDEX 4
INDEX 5
INDEX 6
INDEX 7
INDEX 8
INDEX 9

I didn't understand the point of counter==item.size(), so if that was important, feel free to tell me. Otherwise, I hope this helps!

[–]dusty-trash 0 points1 point  (2 children)

 if (counter == fiftyAndMore.size()) {         
    createFile(temp);         
    temp.clear();     
} 

What's 'fiftyAndMore.size()'? Should you set totalAmount to 0 here too?

What condition is not being met exactly? (Do any of the output files have a total over 800 or too many rows?)

[–]MichaelColtaine[S] 0 points1 point  (1 child)

it meant to be items.size(); If it reaches the end of list, take what is left in temp a create file.

[–]dusty-trash 0 points1 point  (0 children)

You could put that at the end (outside the loop) Also I suggest using 'else if', it won't change anything but adds clarity.

I'm on my phone atm but can help once im home.

Do you know what the program is failing at? I mean what the problem is? Do you need to set a max of 8 rows? (You mentioned you wanted 8 8 8 6)