mercredi 25 novembre 2015

How do I print certain parts of my 2D array in a certain order?

My function needs to take two arguments, called data, player. Argument data is a 2D string array containing the data of the nba.txt spreadsheet. Argument player is a string. The function finds all rows in data for which column 0 (in its lower-case version) contains as a substring the string stored in player (also in its lower-case version). For all such rows, it prints the statistics of the corresponding player.

My Code:

 public static void print_player_info(String[][] data, String player)
  {
   String name = player.toLowerCase();
   for(int i = 0; i<data[0].length; i++)
   {
    String namecolumn = data[i][0];
    String rownames = data[0][i];
    for(int j = 0; j<data[0].length; j++)
    {
        String temp = Arrays.toString(data[j]);
        if(temp.toLowerCase().contains(player))
        {
            System.out.println(rownames+": "+data[j][i]);
        }
    }
   }  
  }

It works in the desired way if I the input only retrieves one person at a time.

Example: input: "lebro" output:

player: LeBron James
games played: 69
minutes per game: 36.1
etc...

if my input happens to retrieve more than one person it prints it stacked:input "jam"

player: James Harden
player: LeBron James
games played: 81
games played: 69
etc...

when I input "jor" it should find multiple names containing "jor" but it outputs nothing. I want to know how to fix this issue along with the previous input to be:

player: James Harden
games played: 81
etc...
player: LeBron James
games played: 69
etc...

Aucun commentaire:

Enregistrer un commentaire