Hello. My task is import data from a text file into an array, and then sort the array and print out the sorted array. Each element of the array represents a clothing item and has name,category,quantity, and price values. I have an Item class to represent a single clothing item, a sorting class with multiple sorting methods, and a class to test the sort and print the array (with the main method). Category values are either "C","W", or "M" (child, women, or men), and i have to sort the array in the order of C, then W, then M. How can i edit my compareTo method to do so? since M comes before W, i do not know how to write my compareTo method to sort the items of category W before the ones of category M.

the data file looks as follows: (if theres a better way to type this out here on these forums, please let me know)

Suit C 12 12.99
Shirt C 25 12.99
Blazer W 16 45.79
Suit M 12 299.00
Suit W 17 127.99
Suit C 12 12.99
Pants W 33 54.99
Pants C 22 10.59
Pants M 12 10.59
Skirt W 17 39.99
Sweater C 25 10.27
Sweater W 17 47.99


heres the code that i have so far:

/**
 * Class ItemTester
 * Creates and prints an array of items
 * Asks user to choose how to sort array
 * Prints sorted array of items
 */
import java.util.Scanner;
import java.io.*;

public class ItemTester
{
   public static void main(String[] args) throws IOException
   {     
      String line;
      Scanner scanToken;  
      Scanner scan = new Scanner(new File("Indata.txt"));
      Item[] itemList = new Item[12];
      String name;
      String category;
      int quantity;
      double price;
      int size = 0;
      while (scan.hasNext())
      {
         line = scan.nextLine();
         scanToken = new Scanner(line);
         scanToken.useDelimiter(" ");            
         name = scanToken.next();
         category = scanToken.next();
         quantity = scanToken.nextInt();
         price = scanToken.nextDouble();
         itemList[size] = new Item(name,category,quantity,price);
         size++;      
      }
      System.out.println("Printout of the array");
      System.out.println();
      System.out.println("Name         Category         Quantity           Price");
      System.out.println();
      for ( int i = 0; i < size; i++)
      {
         System.out.println(itemList[i] + "\t"); 
      }
      System.out.println();  
      System.out.print("Array has " + size + " items");

      System.out.println();
      System.out.println();
      System.out.println();
      System.out.println();

      Sorting.selectionSort(itemList);

      for ( int i = 0; i < size; i++)
      {
         System.out.println(itemList[i] + "\t"); 
      }
    }
}


/**
 * Class Item
 * Represents A Single Item
 */

import java.text.NumberFormat;

public class Item implements Comparable
{
    // instance variables
    public String name;
    public String category;
    public int quantity;
    public double price;

    /**
     * Constructor for objects of class Item
     */
    public Item(String n, String c, int q, double p)
    {
        // initialise instance variables
        name = n;
        category = c;
        quantity = q;
        price = p;
    }

    /**
     * 
     * Methods of class Item
     */
    
   public String toString()
   {    
      NumberFormat fmt =NumberFormat.getCurrencyInstance();
      return name + "\t\t" + category + "\t\t" + quantity + "\t\t" + fmt.format(price); 
   }
   
   public String getName()
   {
       return name;
   }
   
   public String getCategory()
   {
       return category;
   }
   
   public int getQuantity()
   {
       return quantity;
   }
   
   public double getPrice()
   {
       return price;
   }
   
   public int compareTo(Object other)
   {
       int result;
       
       String otherName = ((Item)other).getName();
       String otherCategory = ((Item)other).getCategory();
       int otherQuantity = ((Item)other).getQuantity();
       double otherPrice = ((Item)other).getPrice();
       
       if (category.equals(otherCategory))
            result = name.compareTo(otherName);
       else
            result = category.compareTo(otherCategory);

       
            
       return result;
       
   }
       
       

}


/**
 * Class Sorting
 * Contains 3 different sort methods
 */
public class Sorting
{
   public static void selectionSort (Comparable[] iArray)
   {
       int min;
       Comparable temp;
       
       for (int index = 0; index < iArray.length-1; index++)
       {
           min = index;
           for (int scan = index+1; scan < iArray.length; scan++)
              if (iArray[scan].compareTo(iArray[min]) < 0)
                 min = scan;
                 
          // Swap the values
          temp = iArray[min];
          iArray[min] = iArray[index];
          iArray[index] = temp;
      }
   }
   
   
   public static void insertionSort (Comparable[] iArray)
   {
       for (int index = 1; index < iArray.length; index ++)
       {
           Comparable key = iArray[index];
           int position = index;
           
           // Shift larger values to the right
           while (position > 0 && key.compareTo(iArray[position-1]) < 0)
           {
               iArray[position] = iArray[position -1];
               position--;
           }
           
           iArray[position] = key;
        }
   }



   public static void bubbleSort(Comparable[] iArray)
   {
  
   boolean changed = false;
   for(int a = 0; a < iArray.length - 2; a++)
   {
      if(iArray[a].compareTo(iArray[a + 1]) > 0)
      {
         Comparable tmp = iArray[a];
         iArray[a] = iArray[a + 1];
         iArray[a + 1] = tmp;
         changed = true;
      }
   }

   }

}

If anyone could give me suggestions on how to revise my compareTo() method, it would be greatly appreciated. I need to design it in such a way that items are compared based on category first. (all C before W and all W before M). If two items are in the same category then they are based on quantity. If quantity is also the same, then items are compared based on their name.

Thanks again for any help

Replace Sorting.selectionSort(itemList); With

Arrays.sort(itemList, new Comparator<Item>()
      {
    	  public int compare(Item thisItem, Item otherItem)
    	  {
    		  return thisItem.compareTo(otherItem);
    	  }
      });

and Wallah!

Hello, kbullard516. I was playing around with your code. Here's what I have so far:
(simply I've turned your words in code :P )

public int compareTo(Object other)
{
       String otherName = ((Item)other).getName();
       eCategory otherCategory = ((Item)other).getCategory();
       int otherQuantity = ((Item)other).getQuantity();
       double otherPrice = ((Item)other).getPrice();

       //if category's are equal       
       if (category.equals(otherCategory))
       {
           //if quantity's are equal
           if(quantity == otherQuantity)
           {
               int r = name.compareTo(otherName);
               
               //if names are equal
               if(r == 0)
                    return 0;
               else
                   return r;
           }
           else
                if(quantity > otherQuantity)
                    return 1;
                else
                    return -1;
       }
       else
       {
            return category.compareTo(otherCategory);
       }
}

Almost your code with little addition: I decided to use enum type for category:

enum eCategory
    {
        C,
        W,
        M
    }

That made more simple comparison of categories. So you free to choose: or use that enum

and a bit modify entire prog or add comparison for your categories :)

P.S. That's not the best example, but I think it's better than nothing :P

thanks for the help, i made Category an enum type (makes alot more sense), and changed all of the String declarations of category to eCategory. (i used your code for now). However, im now getting a new problem in my ItemTester class. A delimiter is used to separate the parts of each line in the text document (name, category, quantity, price) by a space.
I do this here in my ItemTester:

while (scan.hasNext())
      {
         line = scan.nextLine();
         scanToken = new Scanner(line);
         scanToken.useDelimiter(" ");            
         name = scanToken.next();
         category = scanToken.next();
         quantity = scanToken.nextInt();
         price = scanToken.nextDouble();
         itemList[size] = new Item(name,category,quantity,price);
         size++;      
      }

however, when i try and compile this class in BlueJ, it says "incompatible types - found Java.Lang.String but expected eCategory" for the line "category = scanToken.next();". i can't figure out how to fix this.

Ok, in this place you can use simple construction, like:

category = eCategory.valueOf(scanToken.next());

i tried putting something like that in before, and get the error when compiling ItemTester "cannot find symbol - constructor Item(java.lang.String,eCategory,int,double)" at the line "itemList = new Item(name,category,quantity,price);" Im not sure why thats occuring because the Constructor for class Item seems fine.

heres an update of my code:


import java.util.Scanner;
import java.io.*;

public class ItemTester
{
   public static void main(String[] args) throws IOException
   {     
      String line;
      Scanner scanToken;  
      Scanner scan = new Scanner(new File("Indata.txt"));
      Item[] itemList = new Item[12];
      String name;
      eCategory category;
      int quantity;
      double price;
      int size = 0;
      while (scan.hasNext())
      {
         line = scan.nextLine();
         scanToken = new Scanner(line);
         scanToken.useDelimiter(" ");            
         name = scanToken.next();
         category = eCategory.valueOf(scanToken.next());
         quantity = scanToken.nextInt();
         price = scanToken.nextDouble();
         itemList[size] = new Item(name,category,quantity,price);
         size++;      
      }
      System.out.println("Printout of the array");
      System.out.println();
      System.out.println("Name         Category         Quantity           Price");
      System.out.println();
      for ( int i = 0; i < size; i++)
      {
         System.out.println(itemList[i] + "\t"); 
      }
      System.out.println();  
      System.out.print("Array has " + size + " items");

      System.out.println();
      System.out.println();
      System.out.println();
      System.out.println();
    }
}



      enum eCategory
      {
        C,
        W,
        M
      }



import java.text.NumberFormat;

public class Item implements Comparable
{
    // instance variables
    public String name;
    public eCategory category;
    public int quantity;
    public double price;

    /**
     * Constructor for objects of class Item
     */
    public Item(String n, eCategory c, int q, double p)
    {
        // initialise instance variables
        name = n;
        category = c;
        quantity = q;
        price = p;
    }

    /**
     * 
     * Methods of class Item
     */
    
   public String toString()
   {    
      NumberFormat fmt =NumberFormat.getCurrencyInstance();
      return name + "\t\t" + category + "\t\t" + quantity + "\t\t" + fmt.format(price); 
   }
   
   public String getName()
   {
       return name;
   }
   
   public eCategory getCategory()
   {
       return category;
   }
   
   public int getQuantity()
   {
       return quantity;
   }
   
   public double getPrice()
   {
       return price;
   }
   
   public int compareTo(Object other)
{
       String otherName = ((Item)other).getName();
       eCategory otherCategory = ((Item)other).getCategory();
       int otherQuantity = ((Item)other).getQuantity();
       double otherPrice = ((Item)other).getPrice();

       //if category's are equal       
       if (category.equals(otherCategory))
       {
           //if quantity's are equal
           if(quantity == otherQuantity)
           {
               int r = name.compareTo(otherName);
               
               //if names are equal
               if(r == 0)
                    return 0;
               else
                   return r;
           }
           else
                if(quantity > otherQuantity)
                    return 1;
                else
                    return -1;
       }
       else
       {
            return category.compareTo(otherCategory);
       }
}

}



public class Sorting
{
   public static void selectionSort (Comparable[] iArray)
   {
       int min;
       Comparable temp;
       
       for (int index = 0; index < iArray.length-1; index++)
       {
           min = index;
           for (int scan = index+1; scan < iArray.length; scan++)
              if (iArray[scan].compareTo(iArray[min]) < 0)
                 min = scan;
                 
          // Swap the values
          temp = iArray[min];
          iArray[min] = iArray[index];
          iArray[index] = temp;
      }
   }
   
   
   public static void insertionSort (Comparable[] iArray)
   {
       for (int index = 1; index < iArray.length; index ++)
       {
           Comparable key = iArray[index];
           int position = index;
           
           // Shift larger values to the right
           while (position > 0 && key.compareTo(iArray[position-1]) < 0)
           {
               iArray[position] = iArray[position -1];
               position--;
           }
           
           iArray[position] = key;
        }
   }
   
   public static void bubbleSort(Comparable[] iArray)
   {
  
   boolean changed = false;
   for(int a = 0; a < iArray.length - 2; a++)
   {
      if(iArray[a].compareTo(iArray[a + 1]) > 0)
      {
         Comparable tmp = iArray[a];
         iArray[a] = iArray[a + 1];
         iArray[a + 1] = tmp;
         changed = true;
      }
   }

   }

 


}

Thanks again for all this help

Well, if follow your's app logic: the Category must be a part of the Item (belongs to it). So if I were you - I would place the definition of it in Item class. And then import it from that class for using in Tester class.
Hm ... sounds strange. But it's the only thing that I can suggest now :)

Comments
Very Helpful

Thank you! It did work after all as is, it just wasnt compiling in BlueJ for some odd reason. Tried it in Command Prompt and it works fine. Thanks alot for your help!

This question has already been answered. Start a new discussion instead.