When I printout the array contents, it prints null rather than the expected entered data. Will someone please tell me what I am doing wrong? Thanks. My code follows;

import java.util.Scanner;
public class Inventory {
		
		public static void main(String[] args)
		{
			Scanner input = new Scanner( System.in );
			boolean isValid = true;//**
			String productName;// = "DVD"//++
			int productNumber;//++
			int onHand;//++
			double price;//++
			//double totalInventory;//++
			//int count;
			double totInventory;//[] = new double[3];//++
			//totalInventory = 0;
			int count = 0;

		while (isValid)
			{
			System.out.println ("Enter Product Name. Enter exit to quit program");
			productName = input.next();

			if (!productName.equalsIgnoreCase ("exit"))//++
			{
				System.out.println ("Enter Item Number: ");
			       productNumber = input.nextInt();
			       System.out.println ("Enter number of items in stock: ");
			       onHand = input.nextInt();
			       System.out.println ("Enter item price: ");
			       price = input.nextDouble();
			       //compute student grade
			       totInventory = (onHand * price);

			     //create a new class and pass the values to the method

			    Product.getData(productName, productNumber, onHand, price, totInventory, count);//+++
			    count++;  //increment the counter to use as the array position in second class//+++
			}
			else
			{
			isValid = false;//**

			System.out.print ("Item Name" + "\t");

			System.out.print ("Item Number" + "\t");

			System.out.print ("Items on Hand" + "\t");

			System.out.print ("Item Price" + "\t");

			System.out.print ("Items Value" + "\t");

			System.out.print ("Total Value" + "\n");

		//Product.printAll(count);
			Product.printArray(count);
	//}

	//Product.modifyArray(count);
		System.out.println ("Thank you, come again.");

			break;

			}

		}
	
		}
}

public class Product {
	static int maxlength = 100;
	//protected static String pName[] = new String[maxlength];
	//protected static int[] iNumber = new int [maxlength];
	//protected static int[] iStock = new int [maxlength];
	//protected static double[] uPrice = new double [maxlength];
	//protected static double[] tInventory = new double [maxlength];
	//protected static double totalInventory = 0;
	static String pName[] = new String[maxlength];
	static int[] iNumber = new int [maxlength];
	static int[] iStock = new int [maxlength];
	static double[] uPrice = new double [maxlength];
	static double[] tInventory = new double [maxlength];
	static double totalInventory = 0;
	double reStockFee = .05;
	double reStockCost = 0;
	double totreStock = 0;
	 public Product(int productNumber, int onHand, double price,
			double totInventory, int count) {
		// TODO Auto-generated constructor stub
	}




	//static String pName[] = new String[maxlength];
	 //static int[] iNumber = new int [maxlength];
	 //static int[] iStock = new int [maxlength];
	 //static double[] uPrice = new double [maxlength];
	 //static double[] tInventory = new double [maxlength];
	 //static double totalInventory = 0;
	 //static double reStockFee = .05;
	 //static double reStockCost = 0;
	 //static double totreStock = 0;
	//load array
	 public static void getData(String productName, int productNumber, int onHand, double price, double totInventory, int count)
	 {
	  //check that count doesn't exceed maximum array length
	  if (count > maxlength)
	  {
	   System.out.println("DATA OVERLOAD!!!!. YOU CRASHED THE SYSTEM!!!  Program Exiting!");
	   System.exit(0);
	  }
	  else
	  {
	   //store values in the array
	   pName[count] = productName;
	   iNumber[count] = productNumber;
	   iStock[count] = onHand;
	   uPrice[count] = price;
	   tInventory[count] = totInventory;
	   totalInventory += totInventory;
	   reStockCost += (totInventory * reStockFee);
	   totreStock = (totalInventory * reStockFee); 
	  // rf.getRestockFee(totInventory);
	    
	  //Public class productFee
	   {
	//	   public double getRestockFee(double total)
		//   {
		 //  double fee = (total * .05);
		   }
	   
	   }
	   
	 }
	 //}




	 //print the values in the array
	//public static void printAll(int count)
	 public static void printArray(int count)
	{
	 for (int i=0; i<count; i++ )
	 {
System.out.print(pName[count]);
System.out.print(iNumber[count]);
System.out.print(iStock[count]);
System.out.print(uPrice[count]);
System.out.print(tInventory[count]);
		  //System.out.println(pName + "\t");;
		  //System.out.println(iNumber + "\t");
		  //System.out.println(iStock + "\t");
		  //System.out.println(uPrice + "\t");
		  //System.out.println(tInventory + "\n");
		 // System.out.printf("%s\n","Restock Fee", reStockCost[count]);
}

	  System.out.printf("%s$%.2f\n", "Entire Inventory Value: ", totalInventory);
	  System.out.printf("%s$%.2f\n", "Entire Inventory Restock Cost: ", totreStock);
	}

//	public static void modifyArray(int count)
{
	//	Arrays.sort(pName);//, iNumber, iStock, uPrice, tInventory);
		//{
			//for (int i=0; i<count; i++ )
				// {

				//	  System.out.println(pName + "\t");
				//	  System.out.println(iNumber + "\t");
				//	  System.out.println(iStock + "\t");
				//	  System.out.println(uPrice + "\t");
		  System.out.println(tInventory + "\n" );
}
}

Recommended Answers

All 10 Replies

Hello, Sandawg.
Here is your mistake: you trying to take element of array, that not exists.
you should use this

public static void printArray(int count)
{
for (int i = 0; i<count; i++ )
{
System.out.print(pName[i]);
...
}

Also there is a couple recommendations to you (including about using static keyword). Ask if you interested.

P.S. If you post your code in more readable state next time, you get the answer much faster :)

P.S. If you post your code in more readable state next time, you get the answer much faster :)

If you notice the OP did try to use code tags, just made a little mistake on the second one.

I saw. But this try didn't helped me to read normally his code. ;)

Thank you so much for your help with this and I am very much interested in your recommendations in regard to the use of static. Also, i tried to use code tags but apparently I did not do it properly, sorry about that. Again, thanks.

Hello, Sandawg.
Beforehand sorry for my english .... :)
Here is my recommendations. Yoy must try to use static variables not so much as you do :) One of the classics said (If I'm not mistaken, it was Troelsen) "Hide everything that you can hide". If you need to wide the using of your variable, you could quickly do it. But if you want to hide everything else, it take much more time.
The "life" of static variable starts, when you are declared it. Like a class. After you declared it, you can use it. Also it's says "the static variable has class level".
And also, about your data: sharply defined you can see the object, with some data grouped in it. So you can create a class, that will contain a data and operations, that you can do with this data. :)
Maybe it seems unnecessary, but try it. You use object-oriented language and do not use it abilities. After a couple times you'll understand that it is really comfortable thing :)

Thank you for your comments and suggestions, unfortunately for me I do not yet know enough about java programming to fully understand what you are telling me. As one can likely imagine, I am taking a required class and continue to struggle mightily with java.

Thank you for your comments and suggestions, unfortunately for me I do not yet know enough about java programming to fully understand what you are telling me. As one can likely imagine, I am taking a required class and continue to struggle mightily with java.

Java is an Object Oriented language: "static" basically means: for this method you do not have to create an instance of a class. there's nothing wrong with using static methods, but you're not really using the benefits the object oriented structure Java offers you.

better approach would be to create a class with the same variables (private, not static) and use setters and getters to get/manipulate their values.

this way, you can have several instances of the class that "remember" the values of the variables at the same time. for instance, every instance of the class can have it's own value for maxLength, while in your code, maxLength will always be 100

Just to add to what I and Stultuske are saying, here is an example for you:

You have method:

public static void printArray(int count)	
{	 
         for (int i=0; i<count; i++ )	 
        {
               System.out.print(pName[count]);
               System.out.print(iNumber[count]);
               System.out.print(iStock[count]);
               System.out.print(uPrice[count]);
               System.out.print(tInventory[count]);		  
        } 	  
        System.out.printf("%s$%.2f\n", "Entire Inventory Value: ", totalInventory);	  
        System.out.printf("%s$%.2f\n", "Entire Inventory Restock Cost: ", totreStock);	
}

(And now try to look at this code not as autor, but as exterior watcher)
Looking on this code, you can't say exactly that you are printing contents of one single object "Product". I see arrays (iNumber, uPrice etc.) but I don't see any connection between them.

Let's see what would be happend, if you will do like Stultuske said:

//we are defining class
public class ProductNew
{
      //with some private declarations, setters, getters etc.
}

//and here we have class Inventory
public class Inventory { 		
public static void main(String[] args)		
{
        //creating an object of newly defined class.
        ProductNew[] warehouse = new ProductNew[maxlength]; 

        //some code

      //and if you ever want to print contents of your array, you'll write:
     for(i=0;i<count;i++) 
    {
             System.out.print(warehouse[i].pName); 
             System.out.print(warehouse[i].iNumber);
            // and so on...
    }

Here is this bond: the fields pName and iNumber are belong to object warehouse (wich is single "Product").
It's not the best example to show abilities of object-oriented language, but It's based on your code, I hope It's more understandable, than my previous post :)

commented: Very helpful +9
commented: just because I was to lazy to put in an example :) but if everyone did what I told them, the world 'd propably have come to an end by now ;) +3

Thanks to both Stultuske and Antenka , and yes, your explanations are helpful. I believe one of the more difficult issues for me is all of the terminology and fitting the various pieces together. Do either of you or anyone have any suggestions on how I go about sorting the arrays? Thanks so much.

Hello Sandawg,
Your thread name is "Printing array contents". If this question is already solved, please mark it solved. About sorting: try to do it yourself, if there are will appear any troubles - create new new thread, describe it (problem) and someone will help you.
Good luck :)

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.