File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes total number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "total number" Watch "total number" New topic
Author

total number

D Wynn
Ranch Hand

Joined: May 03, 2004
Posts: 38
I have a question...I have a java servlet that reads a database and totals up a field off of the database and produces a total dollar amount here is the code...


And this works but if the field contains the same payable amount (example 150.00 is the payable amount for two fields) the amounts are not added together. Am I missing something?
[ October 03, 2005: Message edited by: Mark Spritzler ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

First I used the CODE tags and added indention to make it more readable.

For the inner most if statement there was no curly braces since I assume it was a one line after, but that makes code very unreadable and therefore easy to have problems not knowing which lines of code would run, which probably is leading to your actual problem.

So now, there is a curly brace missing based on probably putting in the curly braces in the wrong place, since it is unreadable to really know.

So please fix where the curly braces should go and you should always use curly braces even with a single line if statement.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6



Just a little rewrite.

Mark
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

This is not a servlet question, I'm moving it to Java in General (beginner)

Dave
Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
Originally posted by D Wynn:
... if the field contains the same payable amount (example 150.00 is the payable amount for two fields) the amounts are not added together
Are you sure that your recordset actually contains duplicate copies of these values? Depending on your query, which you have not shown us, it might only return single instances for duplicate lines. Put some print statements in your loop to confirm that the recordset contents are what you expect.

Also, is there a reason for retrieving all the records, and then adding them up? A better approach would be to do the totalling in the database query.
D Wynn
Ranch Hand

Joined: May 03, 2004
Posts: 38
It was only bringing back one copy of the value so I did a sum of the column in my query and it works!! Thanks for your help. I still wonder why my query wouldn't bring back both copies of the duplicate value??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: total number