Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Actual and formal argument lists differ in length Error

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Actual and formal argument lists differ in length Error

    Hello, i need help with my code.
    I'm trying to compile a method for making phone calls but i get an error that i'm not sure why. (BlueJ)


    Here is the code connected with this method:
        public int getPhoneNumber()
        {
            int phoneNumber = Integer.parseInt(phoneNoField.getText());
            if (phoneNumber == -1)
            {
                JOptionPane.showMessageDialog(frame, "Please use a positive number",
                    "Attention", JOptionPane.WARNING_MESSAGE);
            }
            return phoneNumber;
        }
     
        public int getDuration()
        {
            int theDuration = Integer.parseInt(durationField.getText());
            if (theDuration == -1)
            {
                JOptionPane.showMessageDialog(frame, "Please enter a positive number",
                    "Attention", JOptionPane.WARNING_MESSAGE);
            }
            return theDuration;
        }
     
        public int getDisplayNum()
        {
            int getDisplayNum = -1;
            try {
                getDisplayNum = Integer.parseInt(displayNoField.getText())-1;
                if(getDisplayNum < 0){
                    JOptionPane.showMessageDialog(frame, "Please enter a number greater than 0",
                        "Attention", JOptionPane.WARNING_MESSAGE);
                }
            }
            catch(NumberFormatException e){
                JOptionPane.showMessageDialog(frame, "Please enter an integer",
                    "Attention", JOptionPane.WARNING_MESSAGE);
            }
            return getDisplayNum;
        }
     
        public void phoneCall()
        {
            Gadget mobPhone = Devices.get(getDisplayNum());
            if (getDisplayNum() != -1 && mobPhone instanceof Phone)
            {
                Phone phone = (Phone) Devices.get(getDisplayNum());
                phone.phoneCall(getDisplayNum(), getDuration());
            }
            else {
                JOptionPane.showMessageDialog(frame, "This isn't a Mobile Phone",
                    "Attention", JOptionPane.WARNING_MESSAGE);
            }
        }

    Please let me know if you need more info
    Thank you
    Last edited by JoeBG; April 30th, 2019 at 05:12 PM.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Actual and formal argument lists differ in length Error

    It tells you right there. The phoneCall method does not expect any arguments. But you are calling it like this.

    phone.phoneCall(getDisplayNum(), getDuration());

    It should be called like this.

    phone.phoneCall();

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Actual and formal argument lists differ in length Error

    Well it does compile like this. But i'm trying to connect the method to a button from a GUI. (Make A Call)

    And compiling without them makes the button useless.. I'm not sure how else to do it
    Attached Images Attached Images

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Actual and formal argument lists differ in length Error

    I am unfamiliar with your classes but some of this looks strange.

     public void phoneCall()
        {
            Gadget mobPhone = Devices.get(getDisplayNum());
            if (getDisplayNum() != -1 && mobPhone instanceof Phone)
            {
                Phone phone = (Phone) Devices.get(getDisplayNum());
                phone.phoneCall(getDisplayNum(), getDuration());
            }
            else {
                JOptionPane.showMessageDialog(frame, "This isn't a Mobile Phone",
                    "Attention", JOptionPane.WARNING_MESSAGE);
            }
        }


    Why do you call Devices.get(DisplayNum()) twice? And if mobPhone was an instance of Phone, you could just
    use what was returned.

    Also, I presume that the method you are in, (i.e. phoneCall()), is different that the phone.phoneCall().

    Regards,
    Jim

  5. The Following User Says Thank You to jim829 For This Useful Post:

    JoeBG (May 1st, 2019)

  6. #5
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Actual and formal argument lists differ in length Error

    //A Phone class made for calling using credits and connected with Gadget superclass.
     
    public class Phone extends Gadget
    {
        private int credits;
     
        //Constructor for Phone class extending Gadget superclass.
        public Phone(String themodel, double theprice, int theweight, String thesize, int theCredits)
        {
            super(themodel, theprice, theweight, thesize);
            credits = theCredits;
        }
     
        //Return the current balance of credits.
        public int getCredits()
        {
            return credits;
        }
     
        //Method for inserting more credits into the Phone.
        public void insertCredits(int amount)
        {
            if(amount > 0) {
                credits = credits + amount;
                System.out.println("New credits balance: " +credits);
            }
            else {
                System.out.println("Please use amount greater than " +
                    amount);
            }
        }
     
        //Method for obtaining the gadget info taken from the Superclass Gadget.
        public void gadgetInfo()
        {
            super.gadgetInfo();
            System.out.println("Credits remaining: " +credits);
        }
     
        //Method for using credits to make a phone call.
        public void makeCall(String phoneNumber, int minutes)
        {
            if (credits >= minutes) {
                credits = credits - minutes;
                System.out.println("Number called " +phoneNumber + "\n"+ minutes +" minutes"); 
            }
            else {
                System.out.println("Not enough credits to make the call");
            }
        }    
    }
    This is my Phone class. I've tried moving thing around but i still get the same message. Even if i call DisplayNum only once..

    Thank you

  7. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Actual and formal argument lists differ in length Error

    You don't need to pass getDisplayNum() and getDuration() to phoneCall() as what Jim said.
    Whatever you are, be a good one

Similar Threads

  1. [SOLVED] "Actual and formal argument lists differ in length" HELP!!
    By mattmattmatt in forum Object Oriented Programming
    Replies: 0
    Last Post: October 24th, 2013, 09:21 PM
  2. actual and formal parameters
    By oonagh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2012, 06:49 AM
  3. [SOLVED] actual and formal lists differ in length
    By Appu14 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 28th, 2012, 11:56 PM
  4. ...differ in length error; homework starter code not working.
    By mwebb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 10th, 2011, 12:20 PM
  5. Actual and formal parameters
    By javanoobie in forum Java Theory & Questions
    Replies: 4
    Last Post: December 5th, 2010, 08:29 AM