So I am making a game and have a class Players each player has a name
and score
.
I am trying to get user input for the names of each Player object by calling a function and using for loops and setting the players name from user input from the user.
I have tried a few things while researching still I have a problem.
I saw some people say that Array.toString(arr)
is how you print an array of objects.
I am now getting an error
.\Player.java:13: error: incompatible types: unexpected return value
return "Player: " + name + " Score" + score;
import java.util.*;
class diceGame{
// calling main method
public static void main (String[] args){
createPlayersArray();
}
//Get the details oto launch game such as the number of players
//and collects their names to store in a array
public static void createPlayersArray () {
Scanner kb = new Scanner(System.in);
System.out.println("Please enter the number of players: ");
int numOfPlayers = kb.nextInt();
//Player array to hold all the players
Player [] playerArray = new Player[numOfPlayers];
//Array to set the players names
for (int i = 0; i < numOfPlayers; i ++ ) {
System.out.println("Please enter your name: ");
String currentPlayerName = kb.next();
playerArray[i] = new Player(currentPlayerName, 0);
}
System.out.println("This should be the list of players");
for (int i = 0; i < playerArray.length; i ++ ) {
System.out.println(Array.toString(playerArray));
}
};
}
/////////////////////// Player Class//////////////////////////
class Player{
public String name;
public int score;
public Player (String n, int s){
name = n;
score = s;
}
public void nameAndScore (){
return "Player: " + name + " Score" + score;
}
//Geters and Setter name
public String getName(){
return name;
};
public void setName(){
return name;
}
};
It not so much a error but I am getting the memory address displayed:
Player@880ec60
Player@3f3afe78
Player@7f63425a
Player@36d64342
I would like the console to print:
Tom score: 0
Jerry score: 0