Question

This program is supposed to take user input and determine whether or not it is a perfect number. When I try to compile it, I get the error Method testPerfect in class scalvert_Perfect cannot be applied to given types;

  • testPerfect(num);
  • required :int, int
  • found: int
  • reason: actual and formal argument list differ in length

My code:

import java.util.Scanner;

public class scalvert_Perfect
{
public static void main ( String args[] ) 
{
    Scanner input = new Scanner(System.in);
    int test;
    int num = 0;
    int counter = 0;

    do
    {
        System.out.print("How many numbers would you like to test? ");
        test = input.nextInt();

    }while(test < 1);

    do
    {
        System.out.print("Please enter a possible perfect number: ");
        num = input.nextInt();

        testPerfect(num);
        printFactors(num);

        counter++;

    }while(counter < test);
}


    public static boolean testPerfect(int num, int test)
    {
        int sum = 0;

        for(int i = 0; i < test ; i++)
            {
                if(num % i == 0)
                {
                sum += i;
                }       

            }
                if(sum == num)
                {
                return true;
                }
                else
                {
                return false;
                }
    }       

    public static void printFactors(int num)
    {
        int x;
        int sum = 0;

        for(int factor = num - 1 ; factor > 0; factor--)
            {
                x = num % factor;

                if (x == 0)
                {
                    sum = sum+factor;
                }
            }
                if(sum != num)
                {
                    System.out.printf("%d:NOT PERFECT",num);
                }

            if(sum == num)
                {       
                    System.out.printf("%d: ",num);

                        for(int factor=1; factor < num; factor++)
                            {       
                                x = num % factor;
                                if(x == 0)
                                {
                                    System.out.printf("%d ",factor);
                                }
                            }               
                }
                System.out.print("\n");
                sum = 0;
    }


}
Was it helpful?

Solution

You function requires two integers because of this:

public static boolean testPerfect(int num, int test)

You call it with 1 integer here:

 testPerfect(num);

This is by the way exactly what the error says:

The function:

testPerfect(num);

Needs two integers

required :int, int

But you called it with one:

found: int

So the error is because the amount of arguments is not correct:

reason: actual and formal argument list differ in length

OTHER TIPS

The code should pass test to the testPerfect method, since the signature of the method is testPerfect(int,int). The original call is only passing one int to the method.

 do
    {
        System.out.print("Please enter a possible perfect number: ");
        num = input.nextInt();

        testPerfect(num,test);
        printFactors(num);

        counter++;

    }while(counter < test);

use

testPerfect(int num, int test)

you used one parameter

testPerfect methods takes two parameter int num and int test.

testPerfect(num); should produce compiler error as there is no testPerfect(int) exists.

you can need to pass two paramer Like testPerfect(num, test).

Your function requires two int parameters to be passed to it, yet you only pass it one:

testPerfect(num);

Try passing two to it when you call it.

You're calling this:

public static boolean testPerfect(int num, int test)

like this:

testPerfect(num);

You are either missing a parameter in your call or asking for too many in your method signature.

 public static void main(String[] args) {
    int n=25;
    for(int i=0;i<=n;i++)
    {
        int v= i*i;
        if(v == n)
        {
            System.out.println("it is a perfect number");
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top