# 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 is used to store the amount of "ONE", NoOfDiceArray is used to store the amount of "TWO"................NoOfDiceArray 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;

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++;
break;
case 2:
NoOfDiceArray++;
break;
case 3:
NoOfDiceArray++;
break;
case 4:
NoOfDiceArray++;
break;
case 5:
NoOfDiceArray++;
break;
case 6:
NoOfDiceArray++;
break;
}
}
return NoOfDiceArray;
}

}  Reply With Quote

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]  Reply With Quote

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;

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++;
break;
case 2:
NoOfDiceArray++;
break;
case 3:
NoOfDiceArray++;
break;
case 4:
NoOfDiceArray++;
break;
case 5:
NoOfDiceArray++;
break;
case 6:
NoOfDiceArray++;
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) +
"\nAmount of Two:   " + A.Count(TestArray6) +
"\nAmount of Three: " + A.Count(TestArray6) +
"\nAmount of Four:  " + A.Count(TestArray6) +
"\nAmount of Five:  " + A.Count(TestArray6) +
"\nAmount of Six:   " + A.Count(TestArray6));

}
}```  Reply With Quote

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 04:06 PM.  Reply With Quote

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

## I solved it!!
Thank you.  Reply With Quote

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

## I solved it!!
Thank you.  Reply With Quote

#### Posting Permissions

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