Results 1 to 7 of 7
  1. #1
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default University course, completely baffled by this error!

    Hey im doing some practicals at uni and im managing pretty fine at the moment but i came across this error when compiling this project.

    The issue i have is it says " method load in class Kennel cannot be applied to given types error" i have never had this error before. Most of the code was pre supplied by lecturer we are just practicing methods and such my code is as follows:

    Why can it not call the load method? i tried passing fileName into the load method but still no completion.


    Main class where i run the application:

    Java Code:
     public class Main
    {
          
        public static void main(String args[]) {
            System.out.println("**********HELLO***********");
            KennelDemo demo=new KennelDemo();
            demo.initialise();
            demo.runMenu();
            demo.printAll();
            demo.save();
            System.out.println("***********GOODBYE**********");
        }
    }
    KennelDemo class where i call the methods and host the majority of things. this has the error on "initialise"

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class KennelDemo
    {
        private Dog d;
        private String filename;    //holds the name of the file
        private Kennel kennel;      //holds the kennel
        private Scanner scan; 
        private ArrayList<Dog> dogs; //so we can sore dogs 
    
        public KennelDemo()
        {
            System.out.println("**constructor for KennelDemo");
            scan=new Scanner(System.in);
            System.out.print("Please enter the file name of kennel information: ");
            filename=scan.next();
            System.out.print("enter max number of dogs: ");
            int max=scan.nextInt();scan.nextLine();
            kennel=new Kennel(max);
        }
    
        /**
         * initialise() method runs from the main and reads from a file - calls load(filename) in Kennel
         */
        public void initialise()
        {
            
            
          System.out.println("**method initialise reads from a file");
            System.out.println("Using file "+filename);
            
            
            kennel.load(); 
    
    //as you can see this is where i get my error is says cant call the load method from Kennel, but i have defined it as kennel in kennelDemo constructor!
            
            
    
        }
    Kennel class where method will be run from. i left needless methods out of this post to shorten the post.
    Java Code:
    java.import.util.*;
    java.import.io.*;
    
    public class Kennel {
        private String name;
        private List<Dog> dogs;
        private int nextFreeLocation;
        private int capacity;
    
    public void load(String fileName)throws IOException
        {
            try // prevents crashing if no exsistent file is inputted
            {
    
                {
                    System.out.println("reads using file "+fileName);
                    // put your code here
    
                    Scanner infile =new Scanner(new InputStreamReader
                            (new FileInputStream(fileName)));
                    int num=infile.nextInt();          // counts lines in file ready for loop:
                    for (int i=0;i<num;i++) {      //loops through everyline in the file
                        String name=infile.next(); //check for next string in the file and it becomes name
                        String owner=infile.next(); //check for next string in file it becomes owner
                        boolean bone=infile.nextBoolean(); //check for next boolean this becomes bone
                        Dog d=new Dog(name, owner, bone);  //created dog using these variables from file
                        dogs.add(d); //adds the new dog(d) based off file read and loaded
                    }
                    infile.close();
                }
            }
            catch (IOException e)
            {
                System.out.println("File not found - please try again");
            }
    
        }
    IF ANYONE WANTS A COPY OF THE .JAVA's TO MAYBE SEE THE ERROR IN MORE DETAILS I WILL SUPPLY IF NEEDED. CHEERS.
    Last edited by pbrockway2; 11-07-2012 at 01:51 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: University course, completely baffled by this error!

    Please post the actual and complete error message. Don't paraphrase it as the details are important.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: University course, completely baffled by this error!

    Hi monkeyjr97, welcome to the forums! I've added code tags to your post. When you post code put [code] at the start of each section of code and [/code] at the end. That way the code will retain it's formatting.

    " method load in class Kennel cannot be applied to given types error"
    The message is telling you that you are using load() at the place you indicate in KennelDemo, but when you look in the Kennel class to see how load() is actually defined you find that it must be given an argument. load() loads from a particular file whose name you must specify. In general however a method is defined, you must call it the same way: same spelling, and the same number and types of parameters.

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: University course, completely baffled by this error!

    Sorry about code tag!

    complete error message:

    "method load in class Kennel cannot be applied to given types;

    required: java.land.string; found: no arguments; reason actual and formal arguments differ in length"

    i think it wants me to put some thing like

    Java Code:
     kennel.load(fileName);

    but the thing about that is as you see in the Kennel class, it wants the user to input the name of the file it loads from, so i couldn't call lets say

    Java Code:
     kennel.load(example.txt);
    because the user may not want to load from that file, if this makes sense to you guys i don't know how else to word it.

    i understand that to call a method it must be spelt identically to its name in another class and in Kennel the method is called load, as you can see below.

    Java Code:
     public void load(String fileName)throws IOException

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: University course, completely baffled by this error!

    If this were my project, I'd get the input from the user and load that. Have you tried this?

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: University course, completely baffled by this error!

    pretty new to java in general not exactly sure what you mean, when i run the laod method on its own without calling it it asks for user input of the file name. (is that what you mean?)

    my brief was just to create a method that allows a user to load information from a specific file i think i have done that but calling it is the difficulty.

    is there a way to set is a variable and use a return; to collect the variable then call that?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: University course, completely baffled by this error!

    You're already getting the file name it appears, and you're even printing it out. Why not use that? Just pass that into the method in question.

Similar Threads

  1. Replies: 13
    Last Post: 05-24-2012, 09:50 AM
  2. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 04:43 PM
  3. Baffled while calling method
    By javajames in forum New To Java
    Replies: 11
    Last Post: 05-09-2011, 04:30 PM
  4. Help for University Project
    By ja107 in forum Networking
    Replies: 3
    Last Post: 03-18-2010, 10:22 AM
  5. Help for University Project
    By ja107 in forum Networking
    Replies: 1
    Last Post: 03-04-2010, 12:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •