8 Replies - 7281 Views - Last Post: 05 June 2007 - 06:09 PM Rate Topic: -----

#1 dgilmore286  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 09-May 07

sorting an array of class objects

Post icon  Posted 05 June 2007 - 10:02 AM

Hello,
I am totally new to java programming and am taking a course at an online college, I am not looking to cheat on my homework but I am totally stuck and there seems to be no reference to this issue on the available text provided by the college.
I have created an array of objects, for an inventory application that displays the movie title, quantity in stock, unit price and extended value. I need to find a way to sort the array alphabetically by movie title and the Arrays,sort process is causing errors at runtime and kicking me out. I am not sure what code I should post as the only thing I have so far is the Arrays.sort(products); line and that is not working. Can someone please help guide me to a solution…
This is the error I get when it terminates the application:

Exception in thread “main java.lang.ClassCastException: Product1 cannot be cast
to java.lang.Comparable
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at InventoryArrayTest1.main(InventoryArrayTest1.java:47)

Please let me know what other information is needed, I am not sure what code to post, I do not want to violate any policies by posting code that I should not post….

Thank you so much in advance.

Dave

Is This A Good Question/Topic? 0
  • +

Replies To: sorting an array of class objects

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: sorting an array of class objects

Posted 05 June 2007 - 04:52 PM

Quote

Please let me know what other information is needed, I am not sure what code to post, I do not want to violate any policies by posting code that I should not post…


Hi, post the code you have it is much easier for us to help with the code.
Have you imported:java.util.Arrays;?
And what exactly do you mean by:

Quote

I do not want to violate any policies by posting code that I should not post….

If you are referring to the policies of dream.in.code then it's OK, post as much code as you want. ;)
Was This Post Helpful? 0
  • +
  • -

#3 dgilmore286  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 09-May 07

Re: sorting an array of class objects

Posted 05 June 2007 - 05:17 PM

This is the code that I have so far for the one application. So far I am not having any trouble with the Product1.java code...
I have taken out the line of code "Arrays.sort...
Thank you!



// InventoryTest1.java
// Author: Dave Gilmore
// Created June 4, 2007
// Java application to access class Product1.java. Sets and displays data from that class

import java.util.*;// import utilities

public class InventoryArrayTest1
{
	public static void main ( String args [] )
	{
	   //create 4 instances of class Product1
	   Product1 movieA = new Product1 ( "Dune", 110, 22, 12.99 );
	   Product1 movieB = new Product1( "Evil Dead 2: Dead By Dawn", 220, 14, 9.99 );
	   Product1 movieC = new Product1( "Andromeda Strain, The", 330, 10, 4.99 );
	   Product1 movieD = new Product1( "Underworld: Evolution", 440, 18, 14.99 );


	   //Create Array products and assign elements
	   Product1 products[] = { movieA, movieB, movieC, movieD };

	   //Display objects from array products
	   System.out.printf( "\nInventory of DVD Movies:\n\n" );

	 //Loop to iterate through array, formatting and displaying the contents
	  for ( int counter = 0; counter < products.length; counter++ )
	  {//Block of print commands
		  System.out.printf( "%s\t%s\n", "DVD Title:", products[ counter ].getProdTitle() );//displays title on each iteration

		  System.out.printf( "%s\t\t%04d\n", "Item #:", products[ counter ].getProdNumber() );//displays item number on each iteration

		  System.out.printf( "%s\t%s\n", "Units in Stock:" , products[ counter ].getProdCount() );//displays units in stock on each iteration

		  System.out.printf( "%s\t\t%s%.2f\n", "Price:", "$", products[ counter ].getProdPrice() );//displays product price on each iteration

		  System.out.printf( "%s\t%s%.2f\n\n", "Total Value:", "$", products[ counter ].getProdValue() );//displas product inventory value per iteration

	  }//End print command Block

	  //Process to sort movies aphabetically by title and display in sorted order
	   System.out.printf( "\nSorted Inventory of DVD Movies:\n\n" );

	  //cummulative formula for caluclating and displaying the extended inventory total value
	  double totalValue = 0;

	  for ( int counter = 0; counter < products.length; counter++ )
	  totalValue += products[ counter ].getProdValue();

	  System.out.printf( "%s%s%.2f\n\n", "Total Inventory Value is: ", "$", totalValue );

	}//end main

}//end class InventoryTest1

This post has been edited by William_Wilson: 05 June 2007 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: sorting an array of class objects

Posted 05 June 2007 - 05:27 PM

please remember to use [code] tags
Was This Post Helpful? 0
  • +
  • -

#5 dgilmore286  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 09-May 07

Re: sorting an array of class objects

Posted 05 June 2007 - 05:32 PM

:blink:

Sorry, will from now on...

Thanks,
Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: sorting an array of class objects

Posted 05 June 2007 - 05:39 PM

Well are u still having the problem, why did you remove the Arrays.sort()? Is it working now I don't understand...

View Postdgilmore286, on 5 Jun, 2007 - 05:17 PM, said:

This is the code that I have so far for the one application. So far I am not having any trouble with the Product1.java code...
I have taken out the line of code "Arrays.sort...
Thank you!



// InventoryTest1.java
// Author: Dave Gilmore
// Created June 4, 2007
// Java application to access class Product1.java. Sets and displays data from that class

import java.util.*;// import utilities

public class InventoryArrayTest1
{
	public static void main ( String args [] )
	{
	   //create 4 instances of class Product1
	   Product1 movieA = new Product1 ( "Dune", 110, 22, 12.99 );
	   Product1 movieB = new Product1( "Evil Dead 2: Dead By Dawn", 220, 14, 9.99 );
	   Product1 movieC = new Product1( "Andromeda Strain, The", 330, 10, 4.99 );
	   Product1 movieD = new Product1( "Underworld: Evolution", 440, 18, 14.99 );


	   //Create Array products and assign elements
	   Product1 products[] = { movieA, movieB, movieC, movieD };

	   //Display objects from array products
	   System.out.printf( "\nInventory of DVD Movies:\n\n" );

	 //Loop to iterate through array, formatting and displaying the contents
	  for ( int counter = 0; counter < products.length; counter++ )
	  {//Block of print commands
		  System.out.printf( "%s\t%s\n", "DVD Title:", products[ counter ].getProdTitle() );//displays title on each iteration

		  System.out.printf( "%s\t\t%04d\n", "Item #:", products[ counter ].getProdNumber() );//displays item number on each iteration

		  System.out.printf( "%s\t%s\n", "Units in Stock:" , products[ counter ].getProdCount() );//displays units in stock on each iteration

		  System.out.printf( "%s\t\t%s%.2f\n", "Price:", "$", products[ counter ].getProdPrice() );//displays product price on each iteration

		  System.out.printf( "%s\t%s%.2f\n\n", "Total Value:", "$", products[ counter ].getProdValue() );//displas product inventory value per iteration

	  }//End print command Block

	  //Process to sort movies aphabetically by title and display in sorted order
	   System.out.printf( "\nSorted Inventory of DVD Movies:\n\n" );

	  //cummulative formula for caluclating and displaying the extended inventory total value
	  double totalValue = 0;

	  for ( int counter = 0; counter < products.length; counter++ )
	  totalValue += products[ counter ].getProdValue();

	  System.out.printf( "%s%s%.2f\n\n", "Total Inventory Value is: ", "$", totalValue );

	}//end main

}//end class InventoryTest1

Was This Post Helpful? 0
  • +
  • -

#7 dgilmore286  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 09-May 07

Re: sorting an array of class objects

Posted 05 June 2007 - 05:45 PM

OK, I know I am making this more confusing. The code as posted works fine. Now I need to modify it so that after it displays the movies, it sorts the array and then re-displays it in alphabetical order based on the movie title. There is nothing in our textbook to explain this process. I had found the idea of Arrays.sort but that was not working so I removed it. I just do not know where to go from here. So I guess I am in serious need of some guidance.
Was This Post Helpful? 0
  • +
  • -

#8 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: sorting an array of class objects

Posted 05 June 2007 - 06:06 PM

Now I think I see the prob. OK the Arrays.sort doesn't work for you because when you do Arrays.sort(products) it just sorts them as they were that is it sorts array of objects of the class Product1.
What you want is to sort them by the names(titles of the movies) so you need to create an array of strings in which there would be the titles then you can sort with Arrays.sort
Was This Post Helpful? 0
  • +
  • -

#9 dgilmore286  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 09-May 07

Re: sorting an array of class objects

Posted 05 June 2007 - 06:09 PM

Oooohhhh,

Ok, thank you VERY much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1