-1

I tried different things in my code, but I always get errors.

The instructions for the program is that I need to have a function (besides from main) that receives arguments for an array of integer values and a second argument telling the desire value to look in the array by the user.
It must also return how many times the desire value is repeated on the array.
The errors are related to the counter and also some are in the main function.
I think I am not returning the counter value correctly.

This is my current code:

  import java.util.Scanner;
  import java.util.Arrays;

  public class ArregloBusqueda2
  {
    static int findRepetition(int listOfValues[], int targetValue, int counter)
    {
        int  i;
        boolean found = false;

        for(i=0; i<listOfValues.length; i++)
        {
           while((counter < listOfValues.length))
              {
                 if(listOfValues[i] == targetValue)
                    {
                       counter = counter + 1;
                    }
              }
        }
        return counter;
     }


     public static int main(String[] args)
     {
       Scanner input = new Scanner (System.in);

       int targetValue;
       int listOfValues[] = {1,6,3,8,5,8,3,4,8,3};

       System.out.println("Please enter the desire number to look for: ");
       targetValue=input.nextInt();

       findRepetition(targetValue, counter);


       if(counter != 0)
       {
          System.out.println("The frequency of the number " + targetValue + " is: " + counter);
       }
       else
       {
          System.out.println ("The number " + targetValue + " is not contained     in the array list");
       }
    }
 }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
sprakera
  • 3
  • 2

3 Answers3

2

Multiple issues in your code.

  1. public static int main(String[] args) should be public static void main(String[] args)
  2. findRepetition takes three arguments, but you are passing two agruments
  3. counter variable is not declared
  4. Logical flaw, while((counter < listOfValues.length)) will keep on executing if counter value is less than listOfValues.

     static int findRepetition(int listOfValues[], int targetValue) {
        int i;
        int counter = 0;
    
        for (i = 0; i < listOfValues.length; i++) {
            if (listOfValues[i] == targetValue) {
                counter = counter + 1;
            }
        }
        return counter;
    }
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
    
        int targetValue;
        int listOfValues[] = { 1, 6, 3, 8, 5, 8, 3, 4, 8, 3 };
    
        System.out.println("Please enter the desire number to look for: ");
        targetValue = input.nextInt();
    
        int counter = findRepetition(listOfValues, targetValue);
    
        if (counter != 0) {
            System.out.println("The frequency of the number " + targetValue + " is: " + counter);
        } else {
            System.out.println("The number " + targetValue + " is not contained     in the array list");
        }
    
    }
    
Raghav
  • 4,590
  • 1
  • 22
  • 32
  • 1
    Its finally running. I will keep studying arrays, how to return, etc. so I can keep undestanding every day a little more. – sprakera Nov 26 '16 at 05:41
0

You made your method accept three parameters.

static int findRepetition(int listOfValues[], int targetValue, int counter)

Ask yourself - do you want to "pass in a counter", or only return it? The instructions say the latter.


When you call this method, you are not providing the correct inputs.

findRepetition(targetValue, counter);

What about the listOfValues? You want to findReptition on listOfValues for targetValue, right? So provide the correct parameters into that method call.

Again, you likely do not need the counter passed-in. Because Java is always pass-by-value


Rather, you want to return counter, as you have written. You are looking for this.

int counter = findRepetition(listOfValues, targetValue);

Fixing the remainder of the code is a learning exercise.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

You are not calling findRepetition() with required parameters, findRepetition method takes 3 arguments, but you are passing only 2 arguments.

Prasun
  • 4,943
  • 2
  • 21
  • 23