File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Splitting a string of asterisks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Splitting a string of asterisks" Watch "Splitting a string of asterisks" New topic

Splitting a string of asterisks

E Maton

Joined: Oct 10, 2003
Posts: 3
I need to split a string of asterisks (that will vary in length) with a space after every five asterisks. I have tried playing around w/ a for loop, string tokenizer, and regex and I haven't come up w/ the solution yet.
Yes, I am very new at studying Java! Any ideas?
Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
your problem can be sorted out if you use following code as the last loop..

and few points i want you to note..
1. The following code will print one extra asterics than required

2. Your are using a lot of String mnipulation in loops.. which is not a good idea.. as it will create a lot of Strings in memory. you should use StringBuffer class instead. Eg: -

this is a more efficient way of doing String manipulation..
I think i am clear enough. If stil any queries please revert back..

****************************<br />In 24 hrs Earth rotates once on its Axis.
E Maton

Joined: Oct 10, 2003
Posts: 3
Thanks for taking the time to respond - it's much appreciated!
I corrected the code to output the correct number of asterisks:

For putting a space after every five there is still a problem: The code you gave me outputs 2 less asterisks than there should be (but the format of the asterisks is correct - something like: ***** ***** ***). I tried changing it to output correctly, but nothing has worked so far.
If I change the code to the following (either this or using StringBuffer) then the number of asterisks are correct but it outputs something like: * **** ***** ***
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Try this
Joel McNary

Joined: Aug 20, 2001
Posts: 1824


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
E Maton

Joined: Oct 10, 2003
Posts: 3
Michael and Joel -- both of your ideas worked!! Thanks to everyone for helping me learn more! I will research StringBuffers further so I will know to use them in the future. Here is the final result using both ideas:
I agree. Here's the link:
subject: Splitting a string of asterisks
It's not a secret anymore!