Solved

Behaviour of Java List

Posted on 2014-01-25
3
586 Views
Last Modified: 2014-01-25
Hello there,

I have this requirement to show the amount with thousand separator. I have this POJO class with these two methods which I call according to my requirements
public Integer getTotalAmount() {
    return totalAmount;
}

public String getTotalAmountWithSeparator() {
    return String.format("%,d", totalAmount);
}

public void setTotalAmount(Integer totalAmount) {
    this.totalAmount = totalAmount;
}

Open in new window

now when I use this method getTotalAmountWithSeparator() in my another class which looks like this and I do a println to see if the amount is being shown properly(which it does).


List<SupplierOrderDetails> list = SupplierOrderDetailBussinessLogic.getInstance().getSupplierOrderDetailsFromsupplierOrder(supplierOrder);

      DataProviderBuilder dpb = new DataProviderBuilder();

    // add heading data
    dpb.add("so", supplierOrder.getSupplierOrderNo());
    dpb.add("sn", supplierOrder.getSupplier().getPerName());
    dpb.add("sec", supplierOrder.getSection().getAlternateName());
    dpb.add("od", supplierOrder.getSupplierOrderCreated().toString());

    // add table data
    dpb.addJavaObject(list, "data");

Open in new window


here is the actual method getSupplierOrderDetailsFromsupplierOrder(supplierOrder); which gets the data from the db.


@SuppressWarnings("unchecked")
public List<SupplierOrderDetails> getSupplierOrderDetailsFromsupplierOrder(SupplierOrder supplierOrderDetails){
    Session hibernateSession = HibernateUtills.getInstance().getHibernateSession();
    Criteria criteria = hibernateSession.createCriteria(SupplierOrderDetails.class);
    criteria.add(Restrictions.eq("supplierOrderID", supplierOrderDetails));
    List<SupplierOrderDetails> models = criteria.list();

    System.out.println(" models.size()     " + models.size());

    for (int i = 0; i < models.size(); i++)
    {
        if (models.get(i).getId() != null)
        {
            models.get(i).getProductID().getProductCode();
            models.get(i).getProductID().getBrandName();
            models.get(i).getPurchasePrice();
            models.get(i).getOrderQty();
            models.get(i).getTotalAmountWithSeparator();
            System.out.println(models.get(i).getProductID().getBrandName() + "  TotalAmount      " + models.get(i).getTotalAmountWithSeparator());

        }
        // System.out.println(models.get(i).getPurchasePrice());
    }
    return models;
} 

Open in new window

but when I do a println of the list data here,it does not show the separator in the amount why?????what am I doing wrong
dp = getSupplierOrderData(Long.parseLong(supplierOrderId));
System.out.println("DATA    "+dp.getString("data"));

Open in new window

0
Comment
Question by:zolf
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 39809684
That getTotalAmountWithSeparator() method and the call to format looks correct to me.

So that suggests you're maybe not calling that method from the place in the code where you believe you are calling it.

Is the correct value being printed here:

            System.out.println(models.get(i).getProductID().getBrandName() + "  TotalAmount      " + models.get(i).getTotalAmountWithSeparator());

Open in new window


in your code?

If so, I'm not sure what you are expecting to print here:

System.out.println("DATA    "+dp.getString("data"));

Open in new window


I think you'd need to share the getString("data") method with us - which I don't believe is present?

Also that snippet seems to be calling a different method (getSupplierOrderData) instead of the one you posted above (getSupplierOrderDetailsFromsupplierOrder) so I'm finding it a it hard to piece together exactly where the problem is arising.

Doug
0
 

Author Comment

by:zolf
ID: 39809705
Doug :

thanks for your comments

this is another method which looks like this

private DataProvider getSupplierOrderData(Long supplierorderid)
      {
            SupplierOrder supplierOrder = SupplierOrderBussinessLogic.getInstance().getSupplierOrderById(supplierorderid);
            List<SupplierOrderDetails> list = SupplierOrderDetailBussinessLogic.getInstance().getSupplierOrderDetailsFromsupplierOrder(supplierOrder);

            System.out.println(" list.size()     " + list.size());
            

            for (SupplierOrderDetails supplierOrderDetails : list)
            {
                  Integer totalAmount = supplierOrderDetails.getTotalAmount();
                  System.out.println("totalAmount      !!!!      "+supplierOrderDetails.getTotalAmountWithSeparator());
                  Integer orderQty = supplierOrderDetails.getOrderQty();
                  Integer purchasePrice = supplierOrderDetails.getPurchasePrice();
                  String brandName = supplierOrderDetails.getProductID().getBrandName();
                  Integer productCode = supplierOrderDetails.getProductID().getProductCode();
                  String supplierEngName = supplierOrderDetails.getSupplierOrderID().getSupplier().getEngName();
                  String section = supplierOrderDetails.getSupplierOrderID().getSupplier().getSection().getName();
                  String supplierOrderNo = supplierOrderDetails.getSupplierOrderID().getSupplierOrderNo();
                  Date supplierOrderCreated = supplierOrderDetails.getSupplierOrderID().getSupplierOrderCreated();

                  System.out.println(" supplierOrderDetails     " + supplierOrderDetails.toString());
            }

            DataProviderBuilder dpb = new DataProviderBuilder();

            // add heading data
            dpb.add("so", supplierOrder.getSupplierOrderNo());
            dpb.add("sn", supplierOrder.getSupplier().getPerName());
            dpb.add("sec", supplierOrder.getSection().getAlternateName());

            dpb.add("od", supplierOrder.getSupplierOrderCreated().toString());

            // add table data
            dpb.addJavaObject(list, "data");

            return dpb.getDataProvider();
      }
0
 

Author Comment

by:zolf
ID: 39809706
your comments helped me to find the problem. thanks a lot
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HSSFWorkbook cannot be resolved error 10 70
servlet requestdispatcher include and forward differences 1 31
servlet example 11 40
SHA2 certs for IIS AND Java? 2 91
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question