Search code examples
javaarraysio

reading in file and storing elements in alternate arrays


Say i have a file formatted like this, the first line being the number of elements to be read

4
2
Hearts
10
Spades

How would I go about reading the first 2 lines, storing it in a "Card" object (card value and suit), and putting into one list, and doing the same for the next to lines, but storing it in a separate list, continuing to alternate in this pattern if there are more elements in the file.


Solution

  • Have a counter variable that increments for each card. Use something like the following to alternate between lists:

    if(counter % 2 == 1) {
        oddList.add(card);
    } else {
        evenList.add(card);
    }