# Thread: Counting problem in java

1. Member
Join Date
Jul 2010
Posts
36
Rep Power
0

## Counting problem in java

Hi! I am a beginner in java.
Now I am writing a program about counting.

ArrayOfDice is an array with 5 elements that each element wound be an integer from 1 to 6. Imagine that this array contains 5 dice.

NoOfDiceArray is an array with 6 elements that each element wound be an integer. And, the element is used to store the amount of different facevalue of the five dice. Let's say, NoOfDiceArray[0] is used to store the amount of "ONE", NoOfDiceArray[1] is used to store the amount of "TWO"................NoOfDiceArray[5] is used to store the amount of "SIX".
For example, if the five dice are (1, 2, 1, 3, 6), NoOfDiceArray wound contains {2, 1, 1, 0, 0, 1}.

And I use switch statment to do the counting but there is a bug.
Use the example above, if the five dice are (1, 2, 1, 3, 6), NoOfDiceArray wound contains {2, 1, 1, 0, 0, 1}.
But the actual output is {2, 2, 3, 0, 0, 6}.

if the five dice are (6, 6, 6, 6, 6), the output becomes {0, 0, 0, 0, 0, 30}!!!!!

Here is my code.

public class Counting
{
public int[] NoOfDiceArray = new int[6];

public Counting()
{
int[] NoOfDiceArray = {0, 0, 0, 0, 0, 0};
}

public int[] Count(int[] ArrayOfDice)
{
for (int i = 0; i < ArrayOfDice.length; i++)
{
switch(ArrayOfDice[i])
{
case 1:
NoOfDiceArray[0]++;
break;
case 2:
NoOfDiceArray[1]++;
break;
case 3:
NoOfDiceArray[2]++;
break;
case 4:
NoOfDiceArray[3]++;
break;
case 5:
NoOfDiceArray[4]++;
break;
case 6:
NoOfDiceArray[5]++;
break;
}
}
return NoOfDiceArray;
}

}

2. Ouch. You must have posted the wrong code, that works, but read the Code Conventions regarding the names you use Code Conventions for the Java Programming Language

What is your test harness, as that code is not runnable?

And use CODE tags when posting code. That is: [CODE]... your code goes here ... [/CODE]

3. Member
Join Date
Jul 2010
Posts
36
Rep Power
0
Thz phHein.
Actually I use Test.java to test the Counting.java.

In Test.java, I use TestArray6 = {6, 6, 6, 6, 6} to test the Counting.java.
My expected result is "Amount of Six: 5" but the actual output is "Amount of Six: 30"

Counting.java
Java Code:
```

public class Counting
{
public int[] NoOfDiceArray = new int[6];

public Counting()
{
int[] NoOfDiceArray = {0, 0, 0, 0, 0, 0};
}

public int[] Count(int[] ArrayOfDice)
{
for (int i = 0; i < ArrayOfDice.length; i++)
{
switch(ArrayOfDice[i])
{
case 1:
NoOfDiceArray[0]++;
break;
case 2:
NoOfDiceArray[1]++;
break;
case 3:
NoOfDiceArray[2]++;
break;
case 4:
NoOfDiceArray[3]++;
break;
case 5:
NoOfDiceArray[4]++;
break;
case 6:
NoOfDiceArray[5]++;
break;
}
}
return NoOfDiceArray;
}

}```
Test.java
Java Code:
```public class Test
{
public static void main (String[] args)
{
Counting A;
A = new Counting();

int[] TestArray1 = {1, 1, 1, 1, 1};
int[] TestArray2 = {2, 2, 2, 2, 2};
int[] TestArray3 = {3, 3, 3, 3, 3};
int[] TestArray4 = {4, 4, 4, 4, 4};
int[] TestArray5 = {5, 5, 5, 5, 5};
int[] TestArray6 = {6, 6, 6, 6, 6};

System.out.println ("Amount of One:   " + A.Count(TestArray6)[0] +
"\nAmount of Two:   " + A.Count(TestArray6)[1] +
"\nAmount of Three: " + A.Count(TestArray6)[2] +
"\nAmount of Four:  " + A.Count(TestArray6)[3] +
"\nAmount of Five:  " + A.Count(TestArray6)[4] +
"\nAmount of Six:   " + A.Count(TestArray6)[5]);

}
}```

4. Man, you're calling Count() 6 times with TestArray6 and never reset your counter!! 5*6 = 30

And do us all a favour and read the code conventions!
Last edited by PhHein; 07-07-2010 at 03:06 PM.

5. Member
Join Date
Jul 2010
Posts
36
Rep Power
0
I solved it!!
Thank you.

6. Member
Join Date
Jul 2010
Posts
36
Rep Power
0
I solved it!!
Thank you.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•