-1

I plan to print a message to show the result of data in the array. It suppose to look like
"7 *******" but I got a "7 null*******" instead.

I want to get rid of the "null"

import java.util.ArrayList;    

public class FavorGameDisplay
{
private ArrayList<String> votedGame;
private String[] asterisk;
private int[] gameCount;


/**
 * Constructor for objects of class FavorGameDisplay
 */
public FavorGameDisplay()
{
    FavorGameData data = new FavorGameData();
    votedGame = data.getData();
    analyzeData();
}

/**
 * Analyze and recording counts of the following video games:
 * gameCount[0]: Nier: Automata
 * gameCount[1]: PlayerUnknown’s Battlegrounds
 * gameCount[2]: Wolfenstein 2: The New Colossus
 * gameCount[3]: Cuphead
 * gameCount[4]: any other video games
 */
private void analyzeData()
{
    gameCount = new int[5];
    asterisk = new String[5];
    for (String game : votedGame) 
    {
        if (game.equals("Nier: Automata")) 
        {
            gameCount[0]++;
            asterisk[0] += "*";
        }

        else if (game.equals("PlayerUnknown’s Battlegrounds")) 
        {
            gameCount[1]++;
            asterisk[1] += "*";
        }

        else if (game.equals("Wolfenstein 2: The New Colossus")) 
        {
            gameCount[2]++;
            asterisk[2] += "*";
        }           


        else if (game.equals("Cuphead")) 
        {
            gameCount[3]++;
            asterisk[3] += "*";
        }
        else
        {
            gameCount[4]++;
            asterisk[4] += "*";
        }
    }           
}





/**
 * Display the analyzed data as a histogram
 */
public void displayData()
{

    System.out.println("------------------------------------------");
    System.out.println("Number of people vote for each game: ");        
    System.out.println("Nier: Automata:   " + gameCount[0] + " " + asterisk[0]);
    System.out.println("PlayerUnknown’s Battlegrounds:  " + gameCount[1] + " " + asterisk[1]);
    System.out.println("Wolfenstein 2: The New Colossus:    " + gameCount[2] + " " + asterisk[2]);         
    System.out.println("Cuphead: " + gameCount[3] + " " + asterisk[3]);  
    System.out.println("Others: " + gameCount[4] + " " + asterisk[4]);  
    System.out.println("------------------------------------------");
}
}
2
  • 1
    For starters, you're using parallel arrays; you should instead have a class Game and a List<Game>. Commented Nov 30, 2017 at 0:00
  • @chrylis nah, you don't need to do anything that complicated. You can just do String.join("", Collections.nCopies(count, "*")) when you want to print the asterisks; and store the counts in a Map<String, Integer>. Commented Nov 30, 2017 at 0:09

1 Answer 1

2

You don't initialize the elements of asterisk first; so when you write:

asterisk[0] += "*";

you concatenate the string representation of its current value (null) with "*", so you end up with null*.

Fill the array with empty strings before the loop:

Arrays.fill(asterisk, "");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.