Liz
Liz

Reputation: 25

int cannot be dereferenced, convert an int value from an array to a string

I am writing a program to print out the scores of a bowling game. The scores are provided in an int array called rolls:

rolls = new int[] {1, 1, 2, 2, 3, 3, 4, 4, 5, 4, 6, 3, 2, 7, 1, 8, 0, 9, 0, 0}; 

I am converting the score values to strings in order to print them out because some of the values, like 10 which represents a strike, need to be printed out as strings, like "X" for a value of 10. I am trying to convert the ints to strings and do not know how to fix the error thats occurring, "int cannot be dereferenced." I don't have a lot of experience programming so straightforward answers will be most helpful to me at this point.

public class Bowling {
    private int frames;
    private int[] rolls;
    int currentFrameCount=0;
    int currentFrame=1;
    int grantTotal=0;
    int ballCount=1;

public Bowling(int[] rolls, int frames)
{
   this.frames=frames;
   this.rolls=rolls;
}

public void play()
{
    String box1="";
    String box2="";
    String box3="";
    int totalScore = 0;
    for(int i=0; i < rolls.length; i += 2)
    {
        totalScore += rolls[i] + rolls[i+1];

        if(rolls.length % 2 == 0)
        {
            printNormalFrame(rolls[i].toString(), rolls[i+1].toString(), *totalScore.toString());***-->having the error here**
        }

        else
        {
            printBiggerFrame(rolls[i].toString(), rolls[i+1].toString(), totalScore.toString());
        }

    }
}

public void printNormalFrame(String box1, String box2, String totalScore)
{

   System.out.println("+---+---+");
   System.out.println("| " + box1 + " | " + box2 + " |");
   System.out.println("|---+---|");
   System.out.println("|       " + totalScore + " |"); //todo do the padding correctly using String.Format(...), 
   System.out.println("+---+---+");
}

public void printBiggerFrame(String box1, String box2, String box3, int totalScore)
{
    {
        System.out.println("+---+---+---+");
        System.out.println("| " + box1 + " | " + box2 + " | " + box3 + " |");
        System.out.println("|---+---+---|");
        System.out.println("|      "+totalScore+"|"); //todo the padding     correctly using String.Format(...)
        System.out.println("+---+---+---+");
    }

Upvotes: 1

Views: 1422

Answers (2)

Andrew
Andrew

Reputation: 49606

int is a primitive type, so it has no methods for working with. Try

1. String.valueOf(rolls[i])
2. Integer.toString(rolls[i])
3. rolls[i] + ""
4. new Integer(rolls[i]).toString()

instead of

rolls[i].toString()

Upvotes: 4

Todd
Todd

Reputation: 31710

When you call rolls[i].toString() you are really calling .toString() on an int. Since that method doesn't exist on int, it throws an error.

Possible solutions are to just pass the int directly to your printing methods. Since these are integers, converting to a String isn't going to format it any differently. Another solution would be to use String.valueOf(rolls[i]) to convert your int to a String.

Upvotes: 3

Related Questions