I'm attempting to write a method that allows the user to input the number of players on a sports team, the number of games played, the names of the players, and the amount of points scored per player per game.
Basically, I want the following display output where all information has been provided by the user:
Player Game 1 Game 2 Game 3 Game 4
Tom
Dick
Harry
Matthew
John
... and then points scored by each player in each game.
I admit that this little project originated from a homework assignment, but I've already turned the assignment in and the assignment didn't even ask for user supplied input. I really just want to know how to do this.
Below is my code. A few problems.
I assign each player's name to an array called
playerRoster. Don't all array initialize atarrayName[0]? But when I print outplayerRoster[0], nothing displays. When I print outplayerRoster[1], the first name I enter for the array (which should be indexed atplayerRoster[0]) displays. Why?Related to above: I am prompted to enter the number of players on roster, and say I enter the number 5. But the prompt then allows me to only enter 4 names. Why?
The display above is what I'm looking for, and I'm sure there are built in Java methods for displaying a table of values. I actually don't want that. I kind of want to go forward with loops, like I'm doing now. (I know, I know - beggars can't be choosers)
Here is my code
import java.util.*;
public class BasketballScores
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println ("Enter the number of players on roster: ");
int numPlayers = input.nextInt();
System.out.println();
System.out.println ("Enter the number of games played: ");
int numGames = input.nextInt();
System.out.println();
int[][] arrayScores = new int[numPlayers][numGames];
String[] playerRoster = new String[numPlayers];
System.out.println ("Enter player names: ");
for (int j = 0; j < numPlayers; j++)
{
playerRoster[j] = input.nextLine();
}
for (String element: playerRoster)
{
System.out.println (element);
}
System.out.println();
System.out.println ("Enter the points scored per player per game: ");
System.out.println();
System.out.print (playerRoster[1]);
for (int i = 0; i < numPlayers; i++)
{
for (int k = 0; k < numGames; k++)
{
arrayScores[i][k] = input.nextInt();
}
}
System.out.println();
for (int i = 0; i < numPlayers; i++)
{
System.out.println();
for (int k = 0; k < numGames; k++)
{
System.out.print (arrayScores[i][k] + ", ");
}
}
}
}