# Thread: Counting how many times a value appears in an array?

1. Member
Join Date
Oct 2014
Posts
9
Rep Power
0

## Counting how many times a value appears in an array?

I have this code but I can't seem to get it to work. It keeps saying that "count" cat be found and that it cannot return a value whose type is void.

Java Code:
```public class Cuantos {

static int getPosition(double listOfValues[], double targetValue )
{

int i,count,

position = -1;

for (i=0; i < listOfValues.length; i++)

{
if (listOfValues[i] == targetValue)

{

position = i;

count=count;

}
}

count++;
return position;
return count;

}

static int getPosition2(double listOfValues[], double targetValue )

{
int i,

position = -1;

boolean found = false;

for (i=0; (i < listOfValues.length) && (!found); i++)

{
if (listOfValues[i] == targetValue)

{

position = i;

found = true;

}

}

return position;

}

public static void main(String[] args)

{

double list[] =  {1,6,3,8,5,8,3,4,8,3};

int position;
int count=0;

position = getPosition(list, 3);

if (position != -1)

{

System.out.println("Value found at position "+ position + "\n\n");

}

else

{

}

position = getPosition2(list, 3);

if (position != -1)

{

System.out.println("Value found at position"+ position + "\n\n");

}
else

{

return count;
}
}
}```
Last edited by xlactyu; 11-23-2014 at 08:35 PM.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: Counting how many times a value appears in an array?

I suggest you first edit your post and carefully remove unnecessary lines and indent the braces properly. That code is very hard to read.

Regards,
Jim

#### Posting Permissions

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