# Counting problem in java

• 07-07-2010, 02:15 PM
hei1233212000
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;
}

}
• 07-07-2010, 02:31 PM
PhHein
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]
• 07-07-2010, 02:52 PM
hei1233212000
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
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
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]);                         } }```
• 07-07-2010, 03:04 PM
PhHein
Man, you're calling Count() 6 times with TestArray6 and never reset your counter!! 5*6 = 30