views:

58

answers:

4

I'm trying to update an array element in Java. Some sort of a database like program, but using an array to store data temporarily.

The code seems to be working only on the first array element which is 0. If I try to search for other records, it cannot find them. I don't know why.

boolean blnFound=false;
     String strP=getString("Input product to update: ");


      try{
//loop through the array
    for(int a=0; a<iSl; a++){
      if(strP.compareToIgnoreCase(aProd_name[a])==0){

          //display information match is found
 Display("Product already registered..");
 Display("Product ID: ",aProd_id[a]);
 Display("Product Name: ", aProd_name[a]);
 Display("Product Description: ", aProd_desc[a]);
 Display("Product Size: ", aSize[a]);
 Display("Total Quantity: ", aTotalQty[a]);

  Display("Quantity on hand: ", aQtyonHand[a]);
   Display("Reorder Quantity: ", aReorder[a]);
   Display("Dealer Price: ", aDPrice[a]);
   Display("Selling Price: ", aSPrice[a]);
   Display("Manufacture date: ", aMDate[a]);
   Display("Expiry date: ", aEDate[a]);
   Display("Manufacturer: ", aManufacturer[a]);
 blnFound=true;

Here's the part where it updates:

   //Input new information
  aProd_id[a]=getInteger("Input new  product id: ");

     aProd_desc[a]=getString("Input new product description: ");
     aSize[a]=getString("Input new size: ");
     aTotalQty[a]=getDouble("Input new total quantity: ");
     aQtyonHand[a]=getDouble("Input new quantity on hand: ");
     aReorder[a]=getDouble("Input new reorder: ");
     aDPrice[a]=getDouble("Input new dealer price: ");
     aSPrice[a]=getDouble("Input new selling price: ");
     aMDate[a]=getString("Input new manufactured date: ");
     aEDate[a]=getString("Input new expiration date: ");
     aManufacturer[a]=getString("Input new manufacturer: ");
Display("Product updated!");
A: 

Can't really tell what wrong... Actually, I can't even see where you try to update the array.

However, you probably want to use equalsIgnoreCase instead of compareToIgnoreCase.

aioobe
A: 

You can try to change your for in :

for(int a=0; a<aProd_name.length; a++){

But you code here is incomplete and it's hard to know what you really want to do.

Colin Hebert
A: 

And what is iSl ? When is it defined ? How are the various arrays initialized ? Here is what I would suggest you.

Create a Product class. For this class define as many fields as you have arrays here (id, name, desc, ...). Load Products from your storage and put them in a Collection (a List, a Set, whichever fits your needs). And finally read them. Your code will be easier for you to understand, as you'll have to iterate over only one array, instead of all these ones (one of which I suspect is not initialized with a size greater than 0).

Riduidel
+1  A: 

It looks to me like you are using parallel arrays for storing one type of object. May I recommend a large change? (I'm gonna no matter what you say)

I would create a class called Product or some name that describes the object.

It would look something like this:

public class Product {

    // members storing the data
    private int id;
    private String name;
    private String description;
    private String size;
    private int totalQuant;

    // the rest go here
}

Then, I would store them in a Map<Integer, Product>.

Product someProduct;
Map<String, Product> dataBase = new HashMap<String, Product>();
dataBase.put(someProduct.getName(), someProduct);

That is also how you would 'update' the database. When something is entered that is not already in the database, you just need to create a new object and put it into the Map.

Then you can easily search the Map like so:

boolean productIsRegistered = dataBase.containsKey(strP);
jjnguy