Results 1 to 2 of 2
  1. #1
    xlactyu is offline Member
    Join Date
    Oct 2014
    Posts
    9
    Rep Power
    0

    Default 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 
           
          {
           
           
        System.out.println("Value not found \n\n");
           
          }
           
           position = getPosition2(list, 3);
           
           
          if (position != -1)
           
    {
           
           
      System.out.println("Value found at position"+ position + "\n\n");
           
           
    }
          else
         
     {
             
             
      System.out.println("Value not found \n\n");
             
             return count;
          }
       }
    }
    Please help me find whats wrong with it.
    Last edited by xlactyu; 11-23-2014 at 08:35 PM.

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

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Counting duplicates in an array
    By mar1 in forum New To Java
    Replies: 7
    Last Post: 04-16-2014, 04:06 PM
  2. counting in an array
    By downWithJava in forum New To Java
    Replies: 4
    Last Post: 05-09-2013, 12:29 PM
  3. Replies: 13
    Last Post: 02-24-2013, 01:04 PM
  4. Array for counting from 1 to 100
    By Magic8ballfollower in forum New To Java
    Replies: 5
    Last Post: 09-22-2011, 04:55 AM
  5. Need help with counting strings in an array.
    By dalonehunter in forum New To Java
    Replies: 1
    Last Post: 10-02-2009, 08:47 AM

Posting Permissions

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