Search code examples
javaarraysmethods

method doesn't fill the array with correct values - instead it leaves it at null


I originally simply had a hardcoded array with all the names of the players at each position, but then tried to automate it by splitting it based on two other arrays. I am more of a beginner coder, so I don't understand where it went wrong. Additionally, line 20 was a suggestion by IntelliJ

I had one loop (that got shortened by intelliJ) to figure out how long the array should be and a tracker variable and a for loop. when running through the for loop, if the position of the player at i was the position I am trying to find, then the value of array at tracker would be that player's name, and tracker would be incremented.

public static String[] stringArrayMaker (String[] ADP, int[] pos, int position) {
        int amount = (int) Arrays.stream(pos).filter(po -> position == po).count();
        String[] array = new String[amount];
        int tracker = 0;
        for (int i = 0; i < amount; i++) {
            if (pos[i] == position)
            {
                array[tracker] = ADP[i];
                tracker++;
            }
        }
        return array;
    }

and

String[] ADP = new String[] {"Patrick Mahomes", "Josh Allen", "Jalen Hurts", "Lamar Jackson", "CJ Stroud", "Joe Burrow", "Justin Jefferson", "Ja'Marr Chase", "Justin Herbert", "CeeDee Lamb", "Amon-Ra St. Brown", "Anthony Richardson", "Trevor Lawrence", "Caleb Williams", "Bijan Robinson", "Marvin Harrison Jr.", "Kyler Murray", "Breece Hall", "Puka Nacua", "Jordan Love", "Garrett Wilson", "Tyreek Hill", "Jahmyr Gibbs", "Dak Prescott", "Drake London", "Jayden Daniels", "Tua Tagovailoa", "Malik Nabers", "Drake Maye", "AJ Brown", "Brock Purdy", "Christian McCaffrey", "Sam Laporta", "Chris Olave", "Jaylen Waddle", "Rome Odunze", "Jonathan Taylor", "Trey McBride", "Kyren Williams", "Brandon Aiyuk", "Michael Pittman Jr.", "DJ Moore", "Brock Bowers", "De'Von Achane", "DeVonta Smith", "Nico Collins", "Mark Andrews", "Tank Dell", "Saquon Barkley", "DK Metcalf", "Zay Flowers", "Rashee Rice", "Brian Thomas Jr.", "Travis Etienne Jr.", "Tee Higgins", "Kyle Pitts", "Jordan Addison", "Stefon Diggs", "Jared Goff", "Jaxon Smith-Njigba", "Dalton Kincaid", "JJ McCarthy", "Rachaad White", "Jayden Reed", "Josh Jacobs", "Will Levis", "George Pickens", "Adonai Mitchell", "Christian Kirk", "Mike Evans", "James Cook", "Kenneth Walker III", "Deebo Samuel", "Christian Watson", "TJ Hockenson", "Xavier Worthy", "Ladd McConkey", "Isiah Pacheco", "Keon Coleman", "Davante Adams", "Bryce Young", "Xavier Legette", "Joe Mixon", "Deshaun Watson", "Diontae Johnson", "Troy Franklin", "Amari Cooper", "Baker Mayfield", "Josh Downs", "Kirk Cousins", "Trey Benson", "Jonathon Brooks", "Justin Fields", "Keenan Allen", "George Kittle", "Calvin Ridley", "D'Andre Swift", "Chris Godwin", "Travis Kelce", "Derrick Henry", "Jaylen Wright", "David Njoku", "Cooper Kupp", "Matthew Stafford", "Terry McLaurin", "Javonte Williams", "David Montgomery", "Najee Harris", "Marquise \"Hollywood\" Brown", "Tony Pollard", "Evan Engram", "Tyjae Spears", "Aaron Jones", "Ja'Lynn Polk", "Jakobi Meyers", "Jake Ferguson", "Rhamondre Stevenson", "James Connor", "Blake Corum", "Zack Moss", "Jaylen Warren", "Ricky Pearsall", "Alvin Kamara", "Pat Freiermuth", "Jalen McMillan", "Cole Kmet", "Micheal Mayer", "Brian Robinson Jr.", "Geno Smith", "Zach Charbonnet", "Zamir White", "Chuba Herbert", "Roschon Johnson", "Nick Chubb", "Courtland Sutton", "Aaron Rodgers", "Bo Nix", "Jerry Juedy", "Tyler Allgeier", "Jameson Williams", "Micheal Penix Jr.", "MarShawn Lloyd", "Malachi Corley", "Mike Williams"};
        int[] positions = new int[] {1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 2, 1, 1, 1, 4, 2, 1, 4, 2, 1, 2, 2, 4, 1, 2, 1, 1, 2, 1, 2, 1, 4, 3, 2, 2, 2, 4, 3, 4, 2, 2, 2, 3, 4, 2, 2, 3, 2, 4, 2, 2, 2, 2, 4, 2, 3, 2, 2, 1, 2, 3, 1, 4, 2, 4, 1, 2, 2, 2, 2, 4, 4, 2, 2, 3, 2, 2, 4, 2, 2, 1, 2, 4, 1, 2, 2, 2, 1, 2, 1, 4, 4, 1, 2, 3, 2, 4, 2, 3, 4, 4, 3, 2, 1, 2, 4, 4, 4, 2, 4, 3, 4, 4, 2, 2, 3, 4, 4, 4, 4, 4, 2, 4, 3, 2, 3, 3, 4, 1, 4, 4, 4, 4, 4, 2, 1, 1, 2, 4, 2, 1, 4, 2, 2};
        String[] TE = stringArrayMaker(ADP, positions, 3);

Solution

  • Here is the problem. Change your amount to pos.length in your method. Otherwise, you aren't iterating across all positions.

    for (int i = 0; i < pos.length; i++) { // <----- corrected here.
        if (pos[i] == position) {
            array[tracker] = ADP[i];
            tracker++;
        }
    }
    

    Note that if you use a List<String> to return the results, you don't need to compute the size of the returned array since Lists grow dynamically.