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

    Default How to find how many times a number appears in an array.

    Please help me, this is my code up to now and I can't do anything to make it work. I want it to tell me how many times the number 3 appears, and the last position it was in. I have tried everything and I am ready to give up.

    I am getting errors like"Cuanto.java:88: getPosition(double[],double) in Cuanto cannot be applied to (double)
    a = getPosition(a);" and unreachable statements, and value not found. It's all over the place every time I make a change to it.
    Java Code:
    public class Cuanto { 
    
       static int getPosition(int count,double listOfValues[],
                          
                               double targetValue)
       
       {
          int a = 0, i;
          for (i=0; i < listOfValues.length; i++)
          {
             if (listOfValues[i] == targetValue)
             {
                a++;
             }
          }
          return a;
       }
    
     
       static int getPosition(double listOfValues[],
                          
                               double targetValue)
       {
          int i,a = 0,
             
        position = -1;
             
          for (i=0; i < listOfValues.length; i++)
          {
             
      
            
             
             if (listOfValues[i] == targetValue)
                
                
      {
                
    position = i;
                a = a + 1;
                
             }
          }
              
          return position;
          
              
              
       }
       
      
    
        
       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,
                 a = 0;
          
          
      position = getPosition(list, 3);
          
          
      
          if (position != -1)
      
          {
             
          
        System.out.println("Value found at position "
                
                            + position + "\n\n" + "And is found " + a + " times");
          
    }
          
          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");
             
          }
       }
    }
    Last edited by xlactyu; 12-07-2014 at 02:19 AM.

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

    Default Re: How to find how many times a number appears in an array.

    First, you need to edit your code and reformat it. Get rid of empty lines and unnecessary spaces. Also indent your loops and align the curly braces. Then you need to explain the behavior you are getting.

    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. Replies: 1
    Last Post: 11-23-2014, 07:56 PM
  2. Replies: 13
    Last Post: 02-24-2013, 01:04 PM
  3. Find the First Negative Number of an Array
    By Cod in forum New To Java
    Replies: 12
    Last Post: 02-20-2011, 05:59 PM
  4. Find max number in 2D array?
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 04:27 PM
  5. Replies: 2
    Last Post: 02-16-2008, 08:52 PM

Posting Permissions

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