Skip to content
Advertisement

Why am I getting an error when I try to create the Scoreboard object with an array?

import java.io.*;

public class SaveAndLoad
{
    private Scoreboard[] scoreboard = new Scoreboard[5];
    
    //constructor
    public SaveAndLoad(Scoreboard[] scoreboard)
    {
        this.scoreboard = scoreboard;
    }
    
    public static void save() throws FileNotFoundException
    {
        // 1. Create 5 GameScores
        // 2. Put 5 GameScores into a gamescore[]
        // 3. Create a Scoreboard using a gamescore[]
        
        //Create 5 instances of the Game Class
        GameScore player1 = new GameScore("Rebekah", 199);
        GameScore player2 = new GameScore("Allen", 195);
        GameScore player3 = new GameScore("Sami", 198);
        GameScore player4 = new GameScore("Megan", 142);
        GameScore player5 = new GameScore("Vince", 169);
        
        GameScore[] gamescore = new GameScore[5];
        
        gamescore[0] = player1;
        gamescore[1] = player2;
        gamescore[2] = player3;
        gamescore[3] = player4;
        gamescore[4] = player5;

        //Create an instance of the Scoreboard Class using the GameScore array
        Scoreboard scoreboard = new Scoreboard(GameScore[] gamescore);

The last line in this is giving me an error and I’m not sure why because those are the parameters for the Scoreboard class.

Advertisement

Answer

Short answer

Change to:

Scoreboard scoreboard = new Scoreboard(gamescore);

Longer answer

Consider the code:

new GameScore("Vince", 169);

You are not doing the incorrect

new GameScore(String "Vince", int 169);

So as you know, there is no necessity to pass the variable type as well as the value, and in fact doing so is not only unnecessary but wrong.

Tip

To make you code more concise you may consider writing the code as follows, eliminating the unnecessary variable names.

    GameScore[] gamescore = new GameScore[5];
    
    gamescore[0] = new GameScore("Rebekah", 199);
    gamescore[1] = new GameScore("Allen", 195);
    gamescore[2] = new GameScore("Sami", 198);
    gamescore[3] = new GameScore("Megan", 142);
    gamescore[4] = new GameScore("Vince", 169);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement