method in class cannot be applied to given types
Asked Answered
S

7

29

I'm creating a program that generates 100 random integers between 0 and 9 and displays the count for each number. I'm using an array of ten integers, counts, to store the number of 0s, 1s, ..., 9s.)

When I compile the program I get the error:

RandomNumbers.java:9: error: method generateNumbers in class RandomNumbers cannot be applied to given types;
generateNumbers();

required: int[]

found:generateNumbers();

reason: actual and formal argument lists differ in length

I get this error for the lines of code that I call the methods generateNumbers() and displayCounts() in the main method.

    public class RandomNumbers {

       public static void main(String[] args) {

            //declares array for random numbers
        int[] numbers = new int [99];

        //calls the generateNumbers method
        generateNumbers();

        //calls the displayCounts method        
        displayCounts();
    }

    //***************************************************************** 

    private static int generateNumbers(int[] numbers){

        for(int i = 0; i < 100; i++){
            int randomNumber;
            randomNumber = (int)(Math.random() *10);
            numbers[i] = randomNumber;
        return randomNumber;
        }

    }

    //***************************************************************** 

    private static void displayCounts(int[] numbers){
        int[] frequency = new int[10];

        for(int i = 0, size = numbers.length; i < size; i++ ){
            System.out.println((i) + " counts = " + frequency[i]);
        }

    }//end of displayCounts

    }//end of class
Solley answered 1/11, 2012 at 1:3 Comment(2)
As a side note, generateNumbers returns the value each time when i is 0, so it will always jump out of the loop and never fill the array.Mirk
Another side note: I guess you should also declare your numbers array of size 100, not 99. (int[] numbers = new int [100];) Otherwise you will only print 99 numbers.Lashoh
P
26

generateNumbers() expects a parameter and you aren't passing one in!

generateNumbers() also returns after it has set the first random number - seems to be some confusion about what it is trying to do.

Preen answered 1/11, 2012 at 1:5 Comment(1)
Ok, so I did that, it worked, but it's still producing an error when I call the displayCounts method? I tried passing frequency, but that didn't work...Solley
K
3

call generateNumbers(numbers);, your generateNumbers(); expects int[] as an argument ans you were passing none, thus the error

Kylstra answered 1/11, 2012 at 1:6 Comment(1)
Ok, so I did that, it worked, but it's still producing an error when I call the displayCounts method? I tried passing frequency, but that didn't work...Solley
T
0

The generateNumbers(int[] numbers) function definition has arguments (int[] numbers)that expects an array of integers. However, in the main, generateNumbers(); doesn't have any arguments.

To resolve it, simply add an array of numbers to the arguments while calling thegenerateNumbers() function in the main.

Tindall answered 28/6, 2020 at 8:39 Comment(0)
C
0

I think you want something like this. The formatting is off, but it should give the essential information you want.

   import java.util.Scanner;
public class BookstoreCredit 
{

   public static void computeDiscount(String name, double gpa) 
   {
      double credits;
      credits = gpa * 10;
      System.out.println(name + " your GPA is " +
         gpa + " so your credit is $" + credits);
   
   }

   public static void main (String args[]) 
   {
      String studentName;
      double gradeAverage;
      Scanner inputDevice = new Scanner(System.in);
      System.out.println("Enter Student name: ");
      studentName = inputDevice.nextLine();
      System.out.println("Enter student GPA: ");
      gradeAverage = inputDevice.nextDouble();  
      
      computeDiscount(studentName, gradeAverage);
   }
}
Cockneyism answered 10/9, 2020 at 19:29 Comment(0)
K
0

pass the array as a parameter when call the function, like

(generateNumbers(parameter),displayCounts(parameter))
Kreda answered 29/3, 2021 at 16:32 Comment(0)
P
0

If you get this error with Dagger Android dependency injection, first just try and clean and rebuild project. If that doesn't work, maybe delete the project .gradle cache. Sometimes Dagger just fails to generate the needed factory classes on changes.

Placate answered 6/2, 2022 at 5:27 Comment(0)
S
-1
public class RandomNumbers {

    public static void main(String[] args) {

        //declares array for random numbers
        int[] numbers = new int [100];

        //calls the generateNumbers method
        generateNumbers(numbers);     //passing the empty array

        //calls the displayCounts method        
        displayCounts(numbers);       //passing the array filled with random numbers 
    }

    //***************************************************************** 

    private static void generateNumbers(int[] numbers){

        for(int i = 0; i < 100; i++){
            int randomNumber;
            randomNumber = (int)(Math.random() *10);
            numbers[i] = randomNumber;
        } // here the function doesn't need to return.Since array is non primitive data type the changes done in the function automatically gets save in original array.
    }

    //***************************************************************** 

    private static void displayCounts(int[] numbers){
        int count;     
        for(int i = 0, size = 10; i < size; i++ ){
            count=0;
            for(int j = 0; j < numbers.length ; j++ ){
                if(i == numbers[j])
                count++; //counts each occurence of digits ranging from 0 to 9
            }
            System.out.println((i) + " counts = " + count);
        }

    }//end of displayCounts
}//end of class
Shepley answered 15/8, 2022 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.