aspose file tools*
The moose likes Beginning Java and the fly likes modificating strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "modificating strings" Watch "modificating strings" New topic
Author

modificating strings

Erkki Veps�l�inen
Greenhorn

Joined: Feb 10, 2004
Posts: 9
Hello all!
I am making a program that reads a text file. The file should be modificated so that each line has similar lengths. My question is if I can use stringTokenizer so that the delimiter is the length of a line?
So far I've been trying to solve this with substrings but it gets pretty messy.
Thanks!
Nischal Topno
Ranch Hand

Joined: Nov 24, 2001
Posts: 45
you can use BufferedReader and read the file line by line and put into an arraylist. Also when you are storing the lines read from the file you can have a variable which stores the longest line, say maxlenght.
Then you can add spaces to all the the elements in the arraylist. The number of spaces that you add should be maxlenght - the length of the data stored in the arraylist.
Write back the data to the file line by line, or you can construct a string from the arraylist and write the data to the file.
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Do you want to pad all the lines with spaces so they're equal to the longest line in the file, or do you want to add newlines in such a way as to control what the longest line is?
sev
Erkki Veps�l�inen
Greenhorn

Joined: Feb 10, 2004
Posts: 9
No, i don't want to add spaces but I want the first line decide the maxlength. the rest of lines should be of that length. I'm trying to solve this with arraylist right now. depending on if the other lines are shorter or longer they should be combined or splitted so that all the information is kept. Should I use Arraylist ?
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
I would not use an ArrayList, I'd use a LinkedList. I'd basically do what the first respondent said, use BufferedReader to read the file line by line into a LinkedList, each line added to the end.
Next, go into the first node of the list and get the length of the first line. Say it's 60. Then:
1. Go to next node and look at the contained string.
2. If the line is less than 60, go to next node.
3. If the line is longer than 60:
a. Split that string into two substrings, one consisting of the first 60 characters and the other consisting of the rest.
b. Replace the contents of the current node with the first subsequence.
c. Insert a new node immediately after the current node and place the second subsequence into that node.
4. Go to step 1.
So let's see how this would work. Say your string is "hi\nthere\nu".
You'd read this in so that the nodes in your LinkedList would contain: "hi", "there", "u".
"hi" is two chars long so your line length is 2. Go to second node. "there" is longer than 2, so you split it into "th" and "ere". "th" replace "there" in this node, and you insert a node after this node containing "ere". Go to next node.
This node contains "ere". It's split into "er" and "e", and the "e" is inserted into the next node.
Next node contains "e". It's less than 2, so skip. Last node contains "u", also less than 2, so skip. You now have, in your LinkedList:
"hi", "th", "er", "e", "u"
The reason you want to use a LinkedList instead of an ArrayList is because a LinkedList is ideal for inserting nodes into the middle...it can be done in constant time (no matter how many nodes in the list, a new node can always be added in a fixed amount of time). An ArrayList, on the other hand, can give you access to any data in constant time, but you cannot insert a node anywhere in constant time. Every time you insert into an ArrayList, you push all the contents down one spot, and they all have to be copied one spot down, one at a time.
sev
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Of course, if the input file isn't very big and the entire contents would fit into memory easily, then after the first line, it would be pretty easy to just read the remaining text (all of the remaining lines in the file) into a StringBuffer, and then write them out the desired number of characters at a time.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Erkki Veps�l�inen
Greenhorn

Joined: Feb 10, 2004
Posts: 9
Thanks all, for my purpose StringBuffer was enough, easy & works fine!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: modificating strings