473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sums and StringBuffers

I have developed a parsing program to handle large csv files and
compute sums. The program was

developed and successfully tested using only String objects. I want
replace String objects with

StringBuffer. The file is a record of electricity consumption
collected every fifteen minutes.

The total consumption must be summed.

The file contains duplicate lines and the first spec was to remove
duplicates. This is

accomplished by adding each line as a String object to a HashSet. So
this is where I started in

replacing String with StringBuffer. The problem is that the
computations are wrong when the

StringBuffers replace the Strings in just this one line of code.

Here is the program in pseudo code:

//eliminate duplicate lines
//when lines were added as String objects
//program produced accurate computations

String nextLine = null;
nextLine = getLine();
while(nextLine != null){
hashSet.add(new StringBuffer(nextLine));//change#1
nextLine = getLine();
}
//get the lines
while( //the collection has more elements )
Iterator iterator = hashSet.iterator();
String unitData = null;
unitData = new String ( (StringBuffer)( iterator.next() )
);//change#2
//tokenize the String, extract the consumption and
//add it to a data structure for later output.
//no code changes after this point
}

As a reminder, the program ran perfectly when the change#1 and change#2
were written as follows:

#1 hashSet.add(nextLine);
#2 unitData = iterator.next().toString();

Any help as to why StringBuffer produces bad sums is greatly
appreciated.

Jul 17 '05 #1
2 3297

"guyzdancin" <ma******@guysussman.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I have developed a parsing program to handle large csv files and
compute sums. The program was

developed and successfully tested using only String objects. I want
replace String objects with

StringBuffer. The file is a record of electricity consumption
collected every fifteen minutes.

The total consumption must be summed.

The file contains duplicate lines and the first spec was to remove
duplicates. This is

accomplished by adding each line as a String object to a HashSet. So
this is where I started in

replacing String with StringBuffer. The problem is that the
computations are wrong when the

StringBuffers replace the Strings in just this one line of code.

Here is the program in pseudo code:

//eliminate duplicate lines
//when lines were added as String objects
//program produced accurate computations

String nextLine = null;
nextLine = getLine();
while(nextLine != null){
hashSet.add(new StringBuffer(nextLine));//change#1
nextLine = getLine();
}
//get the lines
while( //the collection has more elements )
Iterator iterator = hashSet.iterator();
String unitData = null;
unitData = new String ( (StringBuffer)( iterator.next() )
);//change#2
//tokenize the String, extract the consumption and
//add it to a data structure for later output.
//no code changes after this point
}

As a reminder, the program ran perfectly when the change#1 and change#2
were written as follows:

#1 hashSet.add(nextLine);
#2 unitData = iterator.next().toString();

Any help as to why StringBuffer produces bad sums is greatly
appreciated.


Hello,

Replacing Strings with StringBuffers is only a good idea when you are
incrementally constructing text. As soon as you see a pattern of

stringVar += anyVar;

repeating multiple times you should start thinking abouty using
StringBuffers. In any other situation you should stick to Strings. This
includes when you want the result of many += operations as a key: use the
toString() value as the key, not the StringBuffer.

Using a mutable object as a key in a Set/Map is dangerous since the key
could be modified after using it as a key. This WILL compromise the
containers integrity.

Regards,

Silvio Bierman
Jul 17 '05 #2
I'm not sure that what silvio was saying is clear enough to answer your
simple question. You're using an object type as a key. The only way to
retrieve the value is to use the same object. When you were using
strings as a key, they're basically considered a value type. You can
then retrieve them using any other string that is equal. This is the
same for adding in duplicates that are supposed to overwrite the
original. That is why silvio just simply said to use the .toString()
return value as the key.

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hunter Hillegas | last post by:
I have just added a third table to a query and I am no longer getting the results I am expecting. Three Tables: CUSTINVOICEJOUR (Header Table) CUSTINVOICETRANS (Line Item Table) MARKUPTRANS...
9
by: rinmanb70 | last post by:
I have a table of transactions, some with past dates, some dated the current date, and some dated in the near future. On a report, I'm looking for a way to get four different sums using the...
8
by: DrewKull | last post by:
Hello, I have a query which sums check boxes (so 0 or -1) then Abs the sum so i can get the number of check box 'checked' per column... based on start and end date. SELECT Abs(Sum(CommCon)) AS ,...
3
by: tpdinh | last post by:
I have to evaluate the sum in the for-loop. How do i do that? Also, I need to print the partial sums using %e. Please help!
5
chunk1978
by: chunk1978 | last post by:
hey... can anyone show me how to calculate sums to the 2nd decimal only? like: 5.50 + 4.00 = 9.50 i seriously have zero idea and could defo use some help...thanks! <script...
8
by: RJMac | last post by:
I’m using ADO 2.6 in VB6 to read in data from a Union query (qryJoinBalanceData) in Access 2000. The Union query contains 3 sub-queries, each of which joins several tables, but they all generate...
1
by: wisemen | last post by:
I have a table with 2 columns, and .I want to run a query that will give me a cumulative sum of the no. of entries that have an <= and on the second column give me a cumulative sum of the no. of...
1
by: someusernamehere | last post by:
Hey people, I need to do some sistem in PHP and MySQL, I have do it a part but the other is the problem, basically is mathemetical operations, te user register something and after insert some...
3
by: theinsider2012 | last post by:
JAVA for Windows XP the question is: Write an application that shows the sum of 1 to n for every n from 1 to 50. That is, the program prints 1 (the sum of 1 alone), 3 (the sum of 1 and 2), 6...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.