0

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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Liam
  • 568
  • 2
  • 15
  • In the first answer to the dupe target question, note that `Arrays.toString()` invokes `toString()` on the objects in the array. Therefore you must implement `toString()` in your `Player` class for this to work. You already have most of it in `nameAndScore()` just return the string instead of printing it. – Jim Garrison Sep 06 '19 at 23:52
  • @ Jim Garrison, I have done this but I am getting an error ` return "Player: " + name + " Score" + score;` – Liam Sep 07 '19 at 00:01
  • @ Jim Garrison, error: incompatible types: unexpected return value – Liam Sep 07 '19 at 00:03
  • Please update the code in your post to reflect the current state of the code. – Jim Garrison Sep 07 '19 at 00:11
  • @ Jim Garrison, I have returned what was being printed in the `nameAndScore()` in class Player. – Liam Sep 07 '19 at 00:20
  • @ Jim Garrison, I got it thanks for the pointers : ) – Liam Sep 07 '19 at 00:35
  • 1
    As to your latest edit: you can't return a string from a `void` method. – Mark Rotteveel Sep 07 '19 at 08:13

0 Answers0