Results 1 to 12 of 12
  1. #1
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Error: Cannot be applied to given types. How do I solve this?

    Hi!

    I finally found a way that may work to sort my array. However, whenever I put the array parameter in the method that is being called I get this error in the InventoryAdvanced class that says:

    method Sort in InventorySort cannot be applied to given types

    How do I solve this? I want to make this method of sorting my array work!

    Java Code:
    	public class InventoryAdvanced {
    
    
    	public static void main(String args[]){
    
    		InventoryDefine swordstone = new InventoryDefine("Sword In The Stone Movie", 1, 10,13.00);
    		InventoryDefine hercules = new InventoryDefine("Hercules", 2, 20, 5.00);
    		InventoryDefine mermaid = new InventoryDefine("The Little Mermaid", 3, 30, 10.00);
         	InventorySort sortThis = new InventorySort();
    
       		InventoryDefine[] inventoryArray = new InventoryDefine[3];
    
       		inventoryArray[0] = swordstone;
       		inventoryArray[1] = hercules;
       		inventoryArray[2] = mermaid;
    
    		sortThis.Sort(inventoryArray);
    
    	}
    }
    Java Code:
    import java.util.Arrays;
    
    public class InventorySort {
    
    	public void Sort(InventoryAdvanced[] array){
    
    	Arrays.sort(array);
    
    	for (int i = 0; i < 3; i++){
    
    	System.out.println("Product:" + i + array[i]);
    
    	}
    
    	}
    }
    Java Code:
    /**
     * @(#)InventoryAdvanced.java
     *
     * InventoryAdvanced application
     *
     * @author
     * @version 1.00 2012/1/26
     */
    
    public class InventoryDefine {
    
    	//Define the variables
    	public String inventoryName;
    	public int inventoryNumber;
    	public int inventoryStock;
    	public double inventoryPrice;
    
    	//Declare the constructor
    	public InventoryDefine(String name, int productNumber, int productStock, double productPrice){
    
    		this.inventoryName = name;
    		this.inventoryNumber = productNumber;
    		this.inventoryStock = productStock;
    		this.inventoryPrice = productPrice;
    
    	}
    }

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

    Default Re: Error: Cannot be applied to given types. How do I solve this?

    Look at you define your sort method:

    Java Code:
    public void Sort(InventoryAdvanced[] array){
    You've coded it to accept an array of InventoryAdvanced objects, and nothing else.

    Now look to see how you're calling it:

    Java Code:
            InventoryDefine[] inventoryArray = new InventoryDefine[3];
     
            inventoryArray[0] = swordstone;
            inventoryArray[1] = hercules;
            inventoryArray[2] = mermaid;
     
            sortThis.Sort(inventoryArray);
    You're calling it by passing in an array of InventoryDefine objects, not InventoryAdvanced objects, and since these don't match, the compiler rightly complains. The fix for this problem should be obvious.

  3. #3
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: Error: Cannot be applied to given types. How do I solve this?

    I found out how to fix it. I replaced InventoryAdvanced with InventoryDefine. However, whenever I try to run the program I get this:

    --------------------Configuration: <Default>--------------------
    Exception in thread "main" java.lang.ClassCastException: InventoryDefine cannot be cast to java.lang.Comparable
    at java.util.ComparableTimSort.countRunAndMakeAscendi ng(ComparableTimSort.java:290)
    at java.util.ComparableTimSort.sort(ComparableTimSort .java:157)
    at java.util.ComparableTimSort.sort(ComparableTimSort .java:146)
    at java.util.Arrays.sort(Arrays.java:472)
    at InventorySort.Sort(InventorySort.java:8)
    at InventoryAdvanced.main(InventoryAdvanced.java:19)

    Process completed.

    What is going on? What did I do wrong here?

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

    Default Re: Error: Cannot be applied to given types. How do I solve this?

    You need to read the Arrays.sort(...) API. If you did, you'd see that it only works for classes that implement the Comparable interface. There two ways to solve this if you must use this method: 1) change the class that's being sorted so that it implements Comparable, or 2) use an overload of the sort method that takes a Comparator object as its second parameter.

  5. #5
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: Error: Cannot be applied to given types. How do I solve this?

    Okay! I implemented the comparable and this is what happens:

    --------------------Configuration: <Default>--------------------
    Product:0InventoryDefine@1f1fba0
    Product:1InventoryDefine@1befab0
    Product:2InventoryDefine@13c5982

    Process completed.


    Also, here is my code:

    Java Code:
    /**
     * @(#)InventoryAdvanced.java
     *
     * InventoryAdvanced application
     *
     * @author
     * @version 1.00 2012/1/26
     */
    
    public class InventoryDefine implements Comparable<InventoryDefine>{
    
    	//Define the variables
    	public String inventoryName;
    	public int inventoryNumber;
    	public int inventoryStock;
    	public double inventoryPrice;
    
    	//Declare the constructor
    	public InventoryDefine(String name, int productNumber, int productStock, double productPrice){
    
    		this.inventoryName = name;
    		this.inventoryNumber = productNumber;
    		this.inventoryStock = productStock;
    		this.inventoryPrice = productPrice;
    
    	}
    
    	public int compareTo(InventoryDefine id) {
        return this.inventoryNumber - id.inventoryNumber;
    }
    }
    What is happening here?

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

    Default Re: Error: Cannot be applied to given types. How do I solve this?

    You are printing out the default String returned from the toString() method for your class. To get a better print out, give your class a new public String toString() method, one that returns a String that makes sense and shows the contents of the object of the class.

    Nice implementation of Comparable by the way!

  7. #7
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: Error: Cannot be applied to given types. How do I solve this?

    Can you show me how that is done with the String? Because I am not sure what you mean. The implementation of the comparable came from someone on these forums who was helping me with it so I can understand it! :)

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

    Default Re: Error: Cannot be applied to given types. How do I solve this?

    It's usually a good idea to give most all your classes a toString override since it makes for a good debugging tool. For instance if I had a class called Foobar that held a String, name and an int, value:

    Java Code:
    public class Foobar {
       private String name;
       private int value;
       public Foobar(String name, int value) {
          this.name = name;
          this.value = value;
       }
       
    //   @Override
    //   public String toString() {
    //      return name + ", " + value;
    //   }
       
       public static void main(String[] args) {
          Foobar[] foobars = new Foobar[3];
          foobars[0] = new Foobar("John", 3);
          foobars[1] = new Foobar("Bill", 4);
          foobars[2] = new Foobar("Fred", 1203);
          
          for (int i = 0; i < foobars.length; i++) {
             System.out.println(foobars[i]);
          }
       }
    }
    If you run this code, you'll see the same type of cryptic output for each Foobar item in the array. If you un-comment the toString() method which returns a String that makes sense, and now run the program, you'll see output that makes much more sense.

  9. #9
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: Error: Cannot be applied to given types. How do I solve this?

    Hey!

    When I do that in my own code I get an error that says:

    Line 29: Error: Cannot find symbol

    Why do I get this? Here is my code:

    Java Code:
    /**
     * @(#)InventoryAdvanced.java
     *
     * InventoryAdvanced application
     *
     * @author
     * @version 1.00 2012/1/26
     */
    
    public class InventoryDefine {
    
    	//Define the variables
    	private String inventoryName;
    	private int inventoryNumber;
    	private int inventoryStock;
    	private double inventoryPrice;
    
    	//Declare the constructor
    	public InventoryDefine(String inventoryName, int inventoryNumber, int inventoryStock, double inventoryPrice){
    
    		this.inventoryName = inventoryName;
    		this.inventoryNumber = inventoryNumber;
    		this.inventoryStock = inventoryStock;
    		this.inventoryPrice = inventoryPrice;
    
    	}
    	
    	  public String toString() {
          return name + ", " + productNumber + productStock + productPrice;
       }
    
    
    }

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

    Default Re: Error: Cannot be applied to given types. How do I solve this?

    The error is telling you the truth. What variable is it saying that it can't find -- well you don't have a variable by that name! Use only the variables that you do in fact have.

  11. #11
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: Error: Cannot be applied to given types. How do I solve this?

    I can't believe I didn't see that! Thank you! My program runs very well!

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

Similar Threads

  1. Replies: 6
    Last Post: 12-11-2011, 11:08 AM
  2. Replies: 3
    Last Post: 10-01-2011, 10:29 PM
  3. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  4. Error: cannot be applied to (java.lang.String)
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 07:33 AM
  5. Replies: 1
    Last Post: 08-01-2007, 07:10 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
  •