• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using StringTokenizer to perform Arithmetic Operations on Strings  RSS feed

 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My program uses the StringTokenizer class to perform arithmetic operation, in this case addition, on String characters. Here's how I've implemented the same:

a) Read the characters from the input stream as a line of text and return a string object (containing the characters)
b) Use the concat() method to combine both strings with space inbetween
c) Create the StringTokenizer object 'st' to split the strings into tokens and
d) Use its methods hasMoreTokens() [checks if there are any tokens from the tokenized string]. If true,
e) Returns the next token after setting the delimiter [delimiter being space]
f) Finally, read the split tokens as integer values and perform arithmetic operation(s)

Given that this process seems to be slightly complicated, is there a more simpler / efficient way of performing arithmetic operations on strings? Would StringBuilder class be appropriate for such string manipulation??

Any response(s) from the forum experts or members is much appreciated.

Thanks,
Sudhir

Program code:
----------------------

 
Sanjeev Kumar Ramineni
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the code attached by you, I assume that your requirement is just to add two numbers. Then there is no need to use String tokenizer.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don’t you use a Scanner and its nextInt() method?
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhir Srinivasan wrote:
Given that this process seems to be slightly complicated, is there a more simpler / efficient way of performing arithmetic operations on strings? Would StringBuilder class be appropriate for such string manipulation??


Simpler : After conversion of Strings to integers, just do arithmetic operations...without using StringTokenizer..
Efficient: it depends on your program usage...
YES, StringBuilder is better than String for manipulation... requiring less memory space!!!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurangkumar Khalasi wrote: . . . YES, StringBuilder is better than String for manipulation... requiring less memory space!!!
How do you know that a StringBuilder requires less memory than a String?

You do not choose operations because of their “efficiency” or their memory footprint, but because of their functionality.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:How do you know that a StringBuilder requires less memory than a String?

Because String is a immutable.
And for each manipulation, there is a need to create different objects.
For example, String s = "Hello"; String s1 = " How are you?";
than for concatanation, we will do operation as
s=s+s1;
and during that new string object with value "Hello How are you?" is created and s will now point to that object rather than "Hello"...
So, we require to create 3 objects and ofcourse "Hello" will be eligible for garbage collection...

But, with StringBuilder(mutable version of String) we can do the same operation with only two objects..with append() method of it...
Thus, for arithmetic operations it will be better than String, based on memory usage...
Campbell Ritchie wrote:You do not choose operations because of their “efficiency” or their memory footprint, but because of their functionality.

May be you are true...But i think it depends on application requirement. If application require efficient usage by covering all required functionality than...
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurangkumar Khalasi wrote: . . . So, we require to create 3 objects and ofcourse "Hello" will be eligible for garbage collection... . . .
No, you are creating one object there, since "Hello" and " How are you?" already exist.
Also "Hello" being a String literal is not eligible for garbage collection.
A StringBuilder defaults to a capacity of 16, unless you specify otherwise to its constructor. You pass "Hello" (5 characters) and append " How are you?" (13 characters), so what will its capacity increase to? Are you sure you are using less memory? And what about S + S1? That creates a StringBuilder behind the scenes, too, so does that use more memory? I can identify at least six objects in the following code
Yes, you use StringBuilder because you need mutable counterparts to String, and that is its functionality. It adds faster execution to that functionality. We are lucky here.

So you see that use of memory can be much more complicated than you think.
But who cares; I can go and take my wife, daughters, sons-in-law and grandsons for dinner and it will cost £xxx. For the same money, I can buy a hard drive with 2TB of capacity. And we are worrying about a few dozen bytes!
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I can identify at least six objects in the following code


Thank You for detailed explanation...
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the outset, thank you all for your reply.

Campbell Ritchie wrote:Why don’t you use a Scanner and its nextInt() method?

Oh yes, I'd completely forgotten about this class. Since my objective is to read the characters input as a String, I guess, the next() would be more appropriate (as the nextInt() would throw up an InputMismatchException).

Campbell Ritchie wrote:
How do you know that a StringBuilder requires less memory than a String?

You do not choose operations because of their “efficiency” or their memory footprint, but because of their functionality.

Yes, you use StringBuilder because you need mutable counterparts to String, and that is its functionality. It adds faster execution to that

functionality. We are lucky here.

When used iteratively, the frequency of object construction, appends and conversions to string is more in "String with + operator" than with StringBuilder. The difference between the two, run-time execution, can be timed to show StringBuilder's superiority over the former.

Campbell Ritchie wrote:A StringBuilder defaults to a capacity of 16, unless you specify otherwise to its constructor. You pass "Hello" (5 characters) and append " How are you?" (13 characters), so what will its capacity increase to? Are you sure you are using less memory?

The capacity() method of this class takes care of any internal buffer overflow. If the number of characters in the StringBuilder exceeds the default capacity, it is automatically increased albeit exponentially. Since this requires resizing of the char[] (from the default 16), the time taken is more pronounced in the case of "String with + operator" given the implicit StringBuilder operation besides the resizing. This would be slower even when compared to a StringBuilder object without size. This also requires a bit of memory. However, you trade-off memory
Campbell Ritchie wrote:So you see that use of memory can be much more complicated than you think.
But who cares; I can go and take my wife, daughters, sons-in-law and grandsons for dinner and it will cost £xxx. For the same money, I can buy a hard drive with 2TB of capacity. And we are worrying about a few dozen bytes!

for performance / functionality as memory can be enhanced, provided cost is not an issue.

I just remembered all this had already been discussed with / explained in detail by Campbell and another moderator in my earlier post which can be found here.


 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the modified code without the StringTokenizer class and using the StringBuilder:



Regards,
Sudhir
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!