method in class cannot be applied to given types

161,716

Solution 1

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.

Solution 2

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

Share:
161,716

Related videos on Youtube

user1368970
Author by

user1368970

Updated on February 06, 2022

Comments

  • user1368970
    user1368970 about 2 years

    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
    
    • Evan Trimboli
      Evan Trimboli over 11 years
      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.
    • Florian Minges
      Florian Minges over 11 years
      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.
  • user1368970
    user1368970 over 11 years
    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...
  • user1368970
    user1368970 over 11 years
    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...