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

    Default My program generates new errors

    I have solved the previous errors in my school assignment. However, my program generates new errors. Can someone please help me solve all these errors so I can get this assignment turned in? Thank you so much :)

    Errors:


    InventoryAdvanced Class:

    Line 14: Incompatible types
    Line 16: Array required, but InventoryDefine found
    Line 17: Array required, but InventoryDefine found
    Line 18: Array required, but InventoryDefine found
    Line 21: method Sort in InventorySort cannot be applied to given types
    Line 26: Array required, but InventoryDefine found

    InventorySort Class:

    Line 22: bad operand types for binary operator
    Line 24: incompatible types
    Line 26: incompatible types


    Java Code:
    	public class InventoryAdvanced {
    
    
    	public static void main(String args[]){
    
    		int size = 4;
    
    		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 methodCall = new InventorySort();
    
       		InventoryDefine inventoryArray = new InventoryDefine[3];
    
       		inventoryArray[0] = swordstone;
       		inventoryArray[1] = hercules;
       		inventoryArray[2] = mermaid;
    
       		//Call the sort method
       		methodCall.Sort(inventoryArray);
    
       		//Display the sorted array
       		for (int i = 0; i < size; i++){
    
       			System.out.println(inventoryArray[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;
    
    	}
    
    }
    Java Code:
    /**
     * @(#)InventorySort.java
     *
     *
     * @author
     * @version 1.00 2012/1/28
     */
    
    
    public class InventorySort {
    
    	public void Sort(InventorySort[] array){
    
    		int a, b, t;
    		int size;
    
    		size = 4;
    
    		for (a=1; a < size; a++)
    			for (b=size-1; b >= a; b--){
    
    				if(array[b-1] > array[b]) {
    
    					t = array[b-1];
    					array[b-1] = array[b];
    					array[b] = t;
    
    				}
    			}
    
    
    }
    
    }

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: My program generates new errors

    You are missing brackets where you declare your inventoryArray

    Java Code:
    InventoryDefine inventoryArray = new InventoryDefine[3]
    should be

    Java Code:
    InventoryDefine[] inventoryArray = new InventoryDefine[3]

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

    Default Re: My program generates new errors

    Thank you! How do I fix the error that says method Sort in class InventorySort cannot be applied to given types? I passed the array correctly, I think.

  4. #4
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: My program generates new errors

    What are you trying to sort? The '>' operator is for comparing numeric values. The way your code is written you are saying "is this object (InventoryDefine) greater than that object" (which doesn't compute in Java). You either need to compare one of the InventoryDefine's fields or implement Comparable and use compareTo() method

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

    Default Re: My program generates new errors

    Thank you! How do I compare InventoryDefines fields? I am trying to sort my array.

  6. #6
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: My program generates new errors

    1. what field are you trying to sort by? price?
    2. You can access an arrays value by arrayName[indexValue] which returns a value, in your case an InventoryDefine object. Therefore, you can access its public fields and methods with dot notation. Something like array[b].inventoryPrice
    3. why are you passing InventorySort[] into your sort method? Don't you want to sort an array of InventoryDefine objects -- InventoryDefine[]?
    4. your declared t as int. This is your temporary object that will hold an object of InventoryDefine. You need to declare it as such.
    5. Why are you hardcoding the size of your arrays. You are going to have issues with the bounds of the array. You can access the length property of an array like yourArrayName.length

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

    Default Re: My program generates new errors

    I am trying to sort by name. I changed the passing to InventoryDefine. How do I declare t as int to hold the object of InventoryDefine? What do you mean by hardcoding? Can you explain further on that? Can you give me an example please? Thank you so much for your help. I greatly appreciate it! I have been stuck on this project for 5 days! How do I do a valid comparison with my arrays in the sort method?

  8. #8
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: My program generates new errors

    I am trying to sort by name. I described how to access the field. The problem you will have is that you are comparing with the '>' operator in your sort algorithm which doesn't work on strings. You will need to look into the String class's compareto method.
    I changed the passing to InventoryDefine.
    How do I declare t as int to hold the object of InventoryDefine? -- You would declare it as InventoryDefine object and not as an int
    What do you mean by hardcoding? int size = 4; is hard coding, you should allow for a varying size array as I described in my earlier post. (Especially because your array's size is 3)
    Can you explain further on that?
    Can you give me an example please? Thank you so much for your help. I greatly appreciate it! I have been stuck on this project for 5 days! Just imagine how nice it will be to finish this on your own.
    How do I do a valid comparison with my arrays in the sort method? -- refer to above.

    Keep at it. The more you do on your own the easier it gets. Good luck.

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

    Default Re: My program generates new errors

    I have a question: Is there anyway to declare a method in the main program and use that method in the main program? Basically, use a method that you declared in a class and then call it in the same class?

Similar Threads

  1. Help with some errors in my program
    By kbud123 in forum Java Applets
    Replies: 5
    Last Post: 05-11-2011, 06:08 AM
  2. A servlet that generates html pages
    By soichi in forum New To Java
    Replies: 2
    Last Post: 01-20-2011, 08:21 AM
  3. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  4. Loading resources generates warning messages
    By Namita Patil in forum New To Java
    Replies: 6
    Last Post: 07-03-2008, 03:04 PM
  5. Generates random maths questions
    By cachi in forum New To Java
    Replies: 4
    Last Post: 08-01-2007, 05:48 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
  •