1

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("+---+---+---+");
    }
2
  • What do you think totalScore.toString() should do? Why do you think so? What did you find when you looked up the error message the compiler is giving you? Commented Apr 8, 2016 at 17:29
  • I thought that using totalScore.toString() would take the int value of my totalScore and convert it to a string so that I can print it out in the manner that I want to. I looked up the error and found comments about using toString to convert an int to a string, which is what I tried, and something about concatination with " " that I didn't understand. I don't know if I should try something like this: System.out.println(Arrays.toString(array)); by importing the util.Arrays package or what. Commented Apr 8, 2016 at 17:33

2 Answers 2

4

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()
Sign up to request clarification or add additional context in comments.

6 Comments

I would discourage the use of #3 and #4.
That did the trick! I assume that String.valueOf(rolls[i]); is taking the value of the array at i and then converting it to a String, is that correct?
@Liz String.valueOf() is overloaded. In this case, it's using the String.valueOf(int), which will convert the int value to a String. To clarify, the array lookup is not done by valueOf(), but by the expression rolls[i], which then passes the single value to the valueOf() method.
@Andreas, yes, I too, but 4-th way looks not better. OP should know all those ways
@AndrewTobilko Sure, and you got my upvote for listing all ways, but not all ways are built equal, and guiding newbie programmers is as much about guiding them away from evil 😈. ;-)
|
3

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.

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.