Hey im working on a code where i need to get a certain column of a 2d array and create a 1d array with the information.

The information im using is a product list in an aray in thsi format [barcode][price][stock]

Now i need to grab the bar codes of the files that are under a certain stock.

I have so far manged to get my code to output the barcodes that are understocked but cannot work out how to put them into an array.

Heres my code so far:

public long[] understockedProductBarcodes(){
		long minStockLevel = 500;
                int maxElements = productBarcodeList.length;
		long[] understockedBarcode;
		understockedBarcode = new long[maxElements];
		int totalColumns = 3;
		int r = 0;
		int c = 0;
		for (r = 0; r < productBarcodeList.length; r++){
			for (c = 2; c < totalColumns; c++){
				if (productBarcodeList[r][c] > 0){
					if (productBarcodeList [r][c] < minStockLevel){
						System.out.println(productBarcodeList[r][0]);
					}
				}
			}
		}
		return understockedBarcode;
	}

I do not think the variable

maxElements

would take the correct length for the array that you are expecting. As this is a multidimension array.
However if you need only to assign the printed values to the array you created it could be done as follows.

public long[] understockedProductBarcodes(){
		long minStockLevel = 500;
                int maxElements = productBarcodeList.length;
		long[] understockedBarcode;
		understockedBarcode = new long[maxElements];
		int totalColumns = 3;
		int r = 0;
		int c = 2;
		int undStocBarCodeCount = 0;
		for (r = 0; r < productBarcodeList.length; r++){
		
			//The For Loop Is Unnecessary
			//for (c = 2; c < totalColumns; c++){
				if (productBarcodeList[r][c] > 0){
					if (productBarcodeList [r][c] < minStockLevel){
						System.out.println(productBarcodeList[r][0]);
						
						// Condition to handle arrayIndexOutOfBoundsExeption
						if(undStocBarCodeCount<maxElements){
							understockedBarcode[undStocBarCodeCount] = productBarcodeList[r][0];
							undStocBarCodeCount++;
						}
					}
				}
			//}
		}
		return understockedBarcode;
	}

Edited 5 Years Ago by Zaad: Commented An Unecessary FOR LOOP

Hey thanks for the reply (:

however i dont think an outofbounds error will occur (tested with code both in and out of condition if and got same result). however it is still not working for me, i think it has something to do with how i am calling the method.

Im calling it with: (prod. is just because im calling it from a different class)

System.out.println("The following barcodes are understocked: " + prod.understockedProductBarcodes())

;

and getting the following output:

The following barcodes are understocked: [J@4f037c71.

However if i just do a simple

System.out.println(understockedBarcode[0]);

it works fine

Edited 5 Years Ago by Episkey: n/a

Yes, if you call

System.out.println(prod.understockedProductBarcodes());

This is similar for you printing

System.out.println(understockedBarcode);

directly it would show the array and not the elements in it. If you need to get the elements to be printed you need to loop through it

for(i=0;i<prod.understockedProductBarcodes().length;i++){
      System.out.println("The following barcodes are understocked: " + prod.understockedProductBarcodes()[i]);
}

wow progress has been made :P,all i did was:

System.out.println("The following barcodes are understocked: " + Arrays.toString(prod.understockedProductBarcodes()));

so simple yet i spent hours trying to figure out what was wrong haha

Oh yours works better in the scheme of things, thankyou so much for your help.

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