I have pretty much gotten my program to run with one little exception. It is asking for the SKU number which has been set but when I run the program it assigns a number to item and does not give me the sku number I had entered in the program.

I have tried various things and nothing seems to work or I wind up with many errors. Can someone please look at my code and tell how to fix it!!!! I am at my whits end....

There are 4 files in the project

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package farmersmarket4;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


/**
 *
 * @author K
 */
class FarmersMarket4 implements ActionListener

{
        static JFrame jfrMain;
	// Declare 4 buttons
	static JButton jbnNext;

	// Declare a panel to draw on - holds labels
	static JPanel jplNavigation;

	// Declare Advanced panel for additional buttons - holds buttons
	static JPanel jplAdvanced;

	// Declare a text area
	static JTextArea jtxtArea;

	// Declare a Label
	static JLabel jlbLabel;

	public FarmersMarket4(){  //constructor
	    // Create and set up the window.
	    jfrMain = new JFrame("Salesman Data Report");
	    jfrMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	    // Add the text area
	    jtxtArea = new JTextArea();
	    jtxtArea.append(Produce.getCurrentProduceString());
	    jfrMain.getContentPane().add(jtxtArea);

	    // Create Panel for label
	    jplNavigation = new JPanel();
            jlbLabel = new JLabel();
            jlbLabel.setText("SALES LEAD PROGRAM");

	    // Create Advanced panel for additional buttons
	    jplAdvanced = new JPanel();

	  
	    // Create Next button
	    jbnNext = new JButton("Next");
	    jbnNext.addActionListener(this);
	    jbnNext.setActionCommand("next");

	   

	    // Add Title label to the panel
            jplNavigation.add(jlbLabel);


	    // Add buttons to Advanced Panel           
	    jplAdvanced.add(jbnNext);
	   
	    

	    

	    // Add panel to frame
	    jfrMain.getContentPane().add(jplNavigation, BorderLayout.PAGE_START);
	    jfrMain.getContentPane().add(jtxtArea, BorderLayout.WEST);
	    
	    // Add Advanced panel to frame
	    jfrMain.getContentPane().add(jplAdvanced, BorderLayout.PAGE_END);

	    //Display the window.
	    jfrMain.pack();
	    jfrMain.setVisible(true);

	}

	private static void showCurrentSales(){
		jtxtArea.setText(Produce.getCurrentProduceString());
	}

	

	public static void enableButtons(boolean enable) {
		jbnNext.setEnabled(enable);
		
		
		//return;
	}

	
	public void actionPerformed(ActionEvent e) {
		// Auto-generated method stub
		if  ("next".equalsIgnoreCase(e.getActionCommand())) {
			Produce.showNext();
			showCurrentSales();
		} 
	}

	


}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package farmersmarket4;

import java.text.NumberFormat;
import java.util.Formatter;
import java.util.Locale;

/**
 *
 * @author K
 */
class ProdDataFormat extends Proddata
{
  
    private String farmName;

	public ProdDataFormat(int SKU, String pName, int pUnits, double pPrice, String fName)
        {
		super(SKU, pName, pUnits, pPrice);
		farmName = fName;
	}

	public ProdDataFormat(int SKU, String pName, int pUnits, double pPrice) 
        {
		super (SKU, pName, pUnits, pPrice);
	}

	public ProdDataFormat(String pName) {
		super(pName);
	}


	public String getFarmName(){
		return farmName;
	}

	public void setFarmName(String fName){
		farmName = fName;
	}

	public double getTotalSales(){
		return super.getTotalSales();
	}

	public double getSalesTax() {
		return super.getTotalSales() * .07;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();


		// Send all output to the Appendable object sb
		Formatter formatter = new Formatter(sb, Locale.US);

		// Explicit argument indices may be used to re-order output.
		formatter.format("SKU: %d\n", getProdSKU());
		formatter.format("Item Name: %s\n", getProdName());
		formatter.format("Farm Name: %s\n", getFarmName());
		formatter.format("Number of Units Sold: %d\n", getProdUnits());
		formatter.format("Produce Price: %s\n", NumberFormat.getCurrencyInstance().format(getProdPrice()));
		formatter.format("Total Sales: %s\n", NumberFormat.getCurrencyInstance().format(getTotalSales()));
		formatter.format("Sales Tax: %s\n", NumberFormat.getCurrencyInstance().format(getSalesTax()));
		formatter.format("Grand Total: %s\n", NumberFormat.getCurrencyInstance().format(getTotalSales() + getSalesTax()));
		return sb.toString();
	}

	


}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package farmersmarket4;

/**
 *
 * @author Kathy
 */

class Proddata 
{
        private int prodSKU;
	private String prodName;
        private int prodUnits;
	private double prodPrice;

	public Proddata(int SKU, String pName, int pUnits, double pPrice)
                
        {
		prodSKU = SKU;
		prodName = pName;                
                prodUnits = pUnits;
		prodPrice = pPrice;

	}
	public Proddata(String pName)
        {
		
		prodName = pName;
                prodSKU = 0;
                prodUnits = 0;
		prodPrice = 0;
	}

	public Proddata() 
        {
		
		prodName = "";
                prodSKU = 0;
                prodUnits = 0;
		prodPrice = 0;
	}

	
    //get SKU
    public int getProdSKU() 
    {
        return prodSKU;
    }

    //set SKU
    public void setProdSKU(int prodSKU) {
        this.prodSKU = prodSKU;
    }

   //get produce name
    public String getProdName() {
        return prodName;
    }

   //set produce name
    public void setProdName(String prodName) {
        this.prodName = prodName;
    }
    
   
    //get number of units sold
    public int getProdUnits() {
        return prodUnits;
    }

    // set units sold
    public void setProdUnits(int prodUnits) {
        this.prodUnits = prodUnits;
    }

    //get produce price
    public double getProdPrice() {
        return prodPrice;
    }

   //set produce price
    public void setProdPrice(double prodPrice) {
        this.prodPrice = prodPrice;
    }


// get total sales
    public double getTotalSales(){
	return prodUnits * prodPrice;
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package farmersmarket4;

import java.text.NumberFormat;
import java.util.Formatter;
import java.util.Locale;

/**
 *
 * @author Kathy
 */
public class Produce 

{
    // Create an array of Produce
	static ProdDataFormat[] farmData = new ProdDataFormat[0]; //array object

	// Keep track of current Produce
	static int currentProduce = 0;


	// main method begins execution of the Java application;
	public static void main( String args[])
	{

                 //loading the array object
		addFarmData("Canteloupe","Kathy' Farm", 12345678,2, 5.00);
		addFarmData("Tomato","Bill's Farm", 87654321, 5, 3.00);
		addFarmData("Potato","Local Farm",12312312, 12, 25.00);
		addFarmData("Squash","Kathy's Farm",20947136, 4,3.00);
		addFarmData("Cucumber","Local Farm",86125390, 22,1.00);
		
		dataSort(); //call a method to sort the data

		
		FarmersMarket4 appGUI = new FarmersMarket4(); //calls constructor

	} // end main

	public static float inventoryValue()
	{
		float total = 0;

		 for(int x = 0; x < farmData.length; x++){
			total += farmData[x].getTotalSales();
		}// end for loop

		return total;
	}

	public static void dataSort()
	{
		  ProdDataFormat tmp;

		   for (int currentProduce = 0; currentProduce < farmData.length; currentProduce++) {
			  for (int i = currentProduce + 1; i< farmData.length; i++) {
				  String s1 = farmData[currentProduce].getProdName();
				  String s2 = farmData[i].getProdName();
				  if( s1.compareTo(s2) > 0)  {
					  tmp = farmData[currentProduce];
					  farmData[currentProduce] = farmData[i];
					  farmData[i] = tmp;
				  }
			  }
		 }
	}

	
	// Return a string for the current Produce

	public static String getCurrentProduceString() {
		if (farmData.length == 0) {
			return "No Produce data";
		} else {
			StringBuilder sb = new StringBuilder();

			// Send all output to the Appendable object sb
			Formatter formatter = new Formatter(sb, Locale.US);

			// Build Inventory String
			formatter.format(farmData[currentProduce].toString());
			formatter.format("\n");
			formatter.format("Total Inventory Value: %s\n", NumberFormat.getCurrencyInstance().format(inventoryValue()));
			return sb.toString();
		}
	}

	// Method to increment current produce
	public static void showNext(){
		if (currentProduce == farmData.length - 1){
			showFirst();
		} else {
			currentProduce++;
		}
	}

	

	// Method to go to first item
	public static void showFirst(){
		currentProduce = 0;
	}

	// Method to go to last item
	public static void showLast(){
		currentProduce = farmData.length - 1;
	}

	//
	public static String[] getStringArray() {
    	String[] currentStringArray = new String[4];
        currentStringArray[2] = Integer.toString(farmData[currentProduce].getProdSKU());
    	currentStringArray[0] = farmData[currentProduce].getProdName();
        currentStringArray[1] = farmData[currentProduce].getFarmName();
        currentStringArray[3] = Integer.toString(farmData[currentProduce].getProdUnits());
        currentStringArray[4] = Double.toString(farmData[currentProduce].getProdPrice());
        return currentStringArray;
    }



	

	

	public static void addFarmData(String name, String farm, int SKU, int units, double price) {
		String[] newProduce = new String[4]; //create a String array
                //load the array with the items that was passed into the method
		newProduce[0] = name;
		newProduce[1] = farm;
                newProduce[2] = Integer.toString(SKU);
		newProduce[2] = Integer.toString(units);  //convert the integer pass in to a string
		newProduce[3] = Double.toString(price);
		addProduce(newProduce); //method  call
		//return;

	}
        
        //assigns the SKU number to the current array object
	public static int getNextFarmData() {
            
		int newCode=0;
		for (int x = 0; x < farmData.length; x++)
                {
                    		
                    if (farmData[x].getProdSKU() > newCode)
                        {
				newCode = farmData[x].getProdSKU();
			}
		}
		return newCode +1;
	}

	// New Method called addProduce
	
	public static void addProduce(String[] data) {
		// Add a new Produce
		ProdDataFormat sDataFormat = new ProdDataFormat(getNextFarmData(), data[0], Integer.valueOf(data[2]), Double.valueOf(data[3]), data[1]);
		Class elementType = farmData.getClass().getComponentType();
		ProdDataFormat[] newArray = (ProdDataFormat[]) java.lang.reflect.Array.newInstance(elementType,farmData.length + 1);
		System.arraycopy(farmData, 0, newArray, 0, farmData.length);

		newArray[newArray.length - 1] = sDataFormat;

		farmData = newArray;
		showLast();
		dataSort();
	}

    	
}

From what I can figure it has something to do with the for loop in the last file that starts on line 142. I don't know what to do to change it to not assign a number to each item and to give me just the sku number that is set in the array.

You need to try debugging your code by adding some printlns to it to show how the values of variables are changed and how the execution flow goes.

This article has been dead for over six months. Start a new discussion instead.