I am trying to get an array sorted but I can't figure out how to correctly write the "if" statement in the sort method. I have been given the task of sorting the arrays by product name. I am not done with the program, but what I do have so far is below. I need some input on the sort method only. The other required elements of this assignment I can manage on my own, but the sort thing has me puzzled.

import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Locale;

public class InventoryPart1//  declares class for InventoryPart1

{
	public static Printer[] sortArray (Printer[] printerProduct)

	        {
				String[] names = new String [printerProduct.length];// step 1
				Printer [] sorted = new Printer[printerProduct.length];// step 2


				for (int i = 0; i < printerProduct.length; i++)// step 3
				{
				 names[i] = printerProduct[i].getProductName();
				}
					for (int i = 0 ; i < printerProduct.length; i++)// step 4
					{
						for (int j = 0; j < names.length; j++)
						{
							if (names[i]= printerProduct.length)
							{
						}
					}

					return sorted;
			}

			}





    public static void main(String args[])//  main method

    {




        Printer printerProduct[] = new Printer[5];

        printerProduct[0] = new Printer(1437, "InkSmart", 17, 29.99);//  new instance of Printer class declared with variable values
        printerProduct[1] = new Printer(2598, "All-In-One", 23, 34.89);
        printerProduct[2] = new Printer(1145, "LaserJet", 14, 24.99);
        printerProduct[3] = new Printer(9956, "PortaPrint", 21, 21.99);
        printerProduct[4] = new Printer(1112, "PageSmart", 15, 59.99);




     }




}
class Printer// class declaration
{

	public int itemNumber;// variable declarations for class Printer
	public String productName;
	public int inStock;
	public double unitPrice;

	public Printer()//  default constructor
	{
		itemNumber = 0;
		productName = "";
		inStock = 0;
		unitPrice = 0;

	}


	public Printer (int itemNumber, String productName, int inStock, double unitPrice)//  declares object's variavle types, and names

	{
		this.itemNumber = itemNumber;
		this.productName = productName;
		this.inStock = inStock;
		this.unitPrice = unitPrice;
	}



	public void setItemNumber (int itemNumber)//  method sets itemNumber variable
	{
		this.itemNumber = itemNumber;
	}

	public int getItemNumber ()//  get method for itemNumber

	{
		return itemNumber;
	}


	public void setProductName (String productName)// set method for productName

	{
		this.productName = productName;
	}


	public String getProductName()//  get method for productName

	{
		return productName;
	}

	public void setInStock(int inStock)// set method for inStock

	{
		this.inStock = inStock;
	}

	public int getInStock()//  get method for inStock

	{
		return inStock;
	}

	public void setUnitPrice (double unitPrice)//  set method for unitPrice

	{
		this.unitPrice = unitPrice;
	}

	public Double getUnitPrice ()//  get method for unitPrice

	{
		return  unitPrice;
	}

	public Double getInventoryValue ()//  get method for inventoryValue. Calculates value of entire inventory

	{
		return  unitPrice * inStock;
	}


	public String toString() {

	return String.format("Item Number=%3d Product Name=%-20s Units In Stock=%d price=%.2f Inventory Value=%.2f",
	itemNumber, productName, inStock, unitPrice, getInventoryValue());
	}

}

As you can see, I have left the sort method in shambles. I have tried several different statement conglomerations for the "if" statement that compares the element in the name array to the productName in the printerProduct array. It seems that no matter what I do I either get "incompatible types" or "required: boolean" error codes.What I am trying to do with it is to sort the elements in the printerProduct array by name and end up displaying them in that order.

How large an array will you need to deal with? If it's a small enough array, you could probably do something simpleminded like go through the original, grab the minimal element, and add it to the new array (easier with ArrayLists, but doable with ordinary arrays). Lather, rinse, repeat.
This is hopeless for anything of any size at all, but it's probably the easiest to code if you've got less than (mumble) pieces of data.
If you want to implement an actual sorting routine, well, the algorithms are out there, as well as sample implementations. Most programmers code these in a second-year course on algorithms, so you know they're not too hard to get your head around.

Or, if you want to just have them sorted, and you don't need to actually write the sorting, look at the Arrays.sort() methods. Make your Printer class implement Comparable, and away you go.

kch1973 , the following comments for your consideration.
1. Since the reference of the array is in fact passed to the sortArray method (i.e. pass by reference), no return value(s) is needed. Therefore the definition of the sorting method would be:
public static void sortArray (Printer[] a){...}
where The argument of the method (which receives the reference of the Printer array) may simple be named as "a" so that the name of the array in the method body is simply called "a". This is only a suggestion to simplify the array name.
2. In the body of the method, the parts 1, 2, 3 should thus be deleted so that the sorting operation is directly made of the array.
3. The comparison for the sorting is made of the names between a pair of adjacent elements. Therefore the "if statement" should be written in the following way:

if ((a[j].getProductName()).compareTo((a[j+1].getProductName()))>0){ //if it is true, swap the two elements
...
}

4. In the main method the sortArray method is thus called by the following code:
sortArray(printerProduct);

5. This is a sorting method called bubble sort. Some improvements can be made for your further reference.

kch1973 , you may find out the code to test the correctness of the sorting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.