1

I have this project that I have been working on, but I'm a bit rusty so I could use some help to figure out this part.

The main jizt of my project, is that I want to be able to fire a gun at a target and have the target analyze my data.

Before I even start on this, I did some research to find out if anyone else had done this before, and turns out a company in USA have done it, but in a different way than what I imagined..

This is not meant as a business idea, just a fun project for my self :)

Now here is my issue, I ofcourse made a "prototype" without the gun, and without the targets. All text in Java to see if I could make this work. I researched a bit since I was rusty (and still am), and figured out most of the problems I faced.

The part that has me stuck is at the very end of it, I have an array for all the shots but I want to divide this array into multiple arrays with the size of the magazine capacity.

How would I accomplish this?

Unfortunately I don't save my code, if I chose to delete it, so my scraps are long gone, but I have tried a few things that came to mind and for several days I havn't been able to figure this one out..

I tried with nested for loops, then I tried again but with nested for loops in a while loop, and I probably tried a few different things aswell.

        if (hasrun == true) {
            //OUTPUT
            ms(2);
            System.out.print("You shot the following:");
            nl(1);
            int counter = 0;
            int i = 0;

            while (sf > magcap) {
                sf-=magcap;
                mnm++;
            }

            while (counter <= mnm) {
                System.out.print("Mag " + mn + ": ");

                for (i+=0; i < magcap; i++) {
                    System.out.print(shots[i] + " ");
                }

                counter++;
                mn++;
                nl(1);
            }

Just to clarify some things in my code here, I know this isn't the cleanest code, I'm not going for clean, and I know I don't need half of my functions (methods), but I did them to practice so they're there. Any help would be greatly appreciated with solving my problem tho! :)

If you want to have a look at all of my code for this project, head over to pastebin

tsgsOFFICIAL
  • 59
  • 2
  • 11
  • 1
    Does this answer your question? [How to split a string array into small chunk arrays in java?](https://stackoverflow.com/questions/27857011/how-to-split-a-string-array-into-small-chunk-arrays-in-java) – default locale Jan 27 '20 at 13:46
  • 1
    I just found this article as well, looks like its the same thing. Will read it right now. – tsgsOFFICIAL Jan 27 '20 at 13:50

3 Answers3

2

You can use Guava to do it easily,

        int[] x = {1,2,3,4,5,6};
        int splitSize =  3;

        List<Integer> list = IntStream.of(x).boxed().collect(Collectors.toList());
        //partition
        List<List<Integer>> partitioned = Lists.partition(list, splitSize);

        //result: [[1,2,3], [4,5,6]]
Shafiul
  • 1,452
  • 14
  • 21
  • I have zero clue what _Guava_ is, could you elaborate a bit on your answer? – tsgsOFFICIAL Jan 27 '20 at 14:11
  • 1
    This is a java library from Google. See installation instruction here: https://github.com/google/guava. Doc: https://github.com/google/guava/wiki or cookbook: https://www.baeldung.com/guava-collections – Shafiul Jan 27 '20 at 14:14
1

Using System.arraycopy:

import java.util.Arrays;

public class Testing {
//  static int[] input = {1,2,3,4,5,6};      // testdata
  static int[] input = {1,2,3,4,5,6,7};    // testdata
//  static int[] input = {1,2,3,4,5,6,7,8};  // testdata
  static int magSize = 3;                  // testdata
//  static int magSize = 5;                  // testdata

  public static void main(String[] args) {
//    int resultSize = (int) Math.ceil((double) input.length / magSize);
    int resultSize = input.length % magSize == 0 ? 
                       input.length/magSize : 
                       input.length/magSize + 1;

    int[][] result = new int[resultSize][magSize];

    for (int i = 0; i < resultSize; i++) {
      System.arraycopy(input,                               // src
                       i*magSize,                           // srcPos
                       result[i],                           // dest
                       0,                                   // destPos
                       (i + 1) * magSize < input.length ? 
                          magSize :
                          input.length - i*magSize);        // length
    }

    System.out.println("Original : " + Arrays.toString(input));
    System.out.println("Result   : " + Arrays.deepToString(result));

/*
prints:
Original : [1, 2, 3, 4, 5, 6, 7]
Result   : [[1, 2, 3], [4, 5, 6], [7, 0, 0]]
*/
  }
}
Scratte
  • 3,056
  • 6
  • 19
  • 26
  • Any chance you'd be able to pinpoint my error in this code? Link: https://pastebin.com/aQQu63xy – tsgsOFFICIAL Jan 28 '20 at 12:00
  • @tsgsOFFICIAL The general idea is to create a new Question, if you have a new Question. Although I generally go out of my way to help people, it doesn't make sense for me to debug your program without a clear understanding of what it's doing compared to what it's suppose to do. I'm not altruistic enough to not welcome upvotes or my answer being accepted though. I imagine you'll risk downvotes for a "Please fix my code" Question, so it's a bit tricky. Debugging can be a time consuming thing, but it's the best approach for your problem. Use an IDE or print statements. – Scratte Jan 28 '20 at 12:26
0

I figured it somewhat out, here is what I did. Not quite implemented yet, but this worked with an array.

// OUTPUT
            while (sf >= magcap) {
                sf -= magcap;
                mn++;
            }
            ms(2);
            System.out.println(mn + " mags and " + sf + " shots fired");
            nl(2);
            for (int i = 0; i < mn; i++) {
                from = magcap * magcounter - magcap;
                to = magcap * magcounter;
                System.out.print("Mag " + magcounter + ": ");
                for (int j = from; j < to - 1; j++) {
                    System.out.print(shots[j] + ", ");
                }
                System.out.print(shots[to - 1] + ".");
                magcounter++;
                nl(1);
            }
            // PRINT THE REST
            if (sf >= 1) {
                from = magcap * magcounter - magcap;
                to = magcap * magcounter;
                if (sf == 1) {
                    System.out.print("Mag " + magcounter + ": ");
                    System.out.print(shots[from] + ".");
                } else {
                    System.out.print("Mag " + magcounter + ": ");
                    for (int i = 0; i < sf - 1; i++) {
                        System.out.print(shots[from + i] + ", ");
                    }
                    from += sf -1;
                    System.out.print(shots[from] + ".");
                }
            }

The output is

1 2 3 4 5 6 7 8 9 10 
3 mags fired and 1 shots fired

Mag 1: 1 2 3 
Mag 2: 4 5 6 
Mag 3: 7 8 9 
Mag 4: 10 
tsgsOFFICIAL
  • 59
  • 2
  • 11