We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › txt word-wrap removal
Page Index Toggle Pages: 1
txt word-wrap removal (Read 1125 times)
txt word-wrap removal
Aug 27th, 2009, 3:07pm
 
Hello guys!
 I'm trying to make sketch with simple function of  word-wrap removal in txt files.
 The following code reads, join strings, writes, but not as it needs:
 I tried to make this additionally: when you give parameters how many old strings will be in 1 new - 5 or 10 old in 1 new string. Now the problem is: It writes to document only 2 new strings( last 7 joined old strings into 8th new), and new others it doesn't create. I am afraid I get confused because of "loops" and "may not have been initialized".
 This i use when I make 7 strings in one new:

for(int i = 1; i < lines.length/7 ; i++) { ... }
                     
 Next code for removing array out of bound exception - so the last or first 7 strings must be empty(spaced once and wrapped).

for(int ii = 0; ii <lines.length-6 ; ii++) {          
                                           ...
mystring = lines[ii] + lines[ii+1] + lines[ii+2] + lines[ii+3] + lines[ii+4] + lines[ii+5] + lines[ii+6];
                                            ...       }

Full Code:



String sevenStrings = new String();
String temp[];
String[] bigStrings;
String lines[] = loadStrings("loadsomefile.txt");
String[] neww= new String[lines.length/7];
for(int i = 1; i < lines.length/7 ; i++) {
 String currentstring;
 for(int ii = 0; ii <lines.length-6 ; ii++) {
   currentstring = lines[ii];
   String  mystring;
   mystring = lines[ii] + lines[ii+1] + lines[ii+2] + lines[ii+3] + lines[ii+4] + lines[ii+5] + lines[ii+6];
   String[] SevenStrings = {
     lines[i], lines[i+1], lines[i+2], lines[i+3], lines[i+4], lines[i+5], lines[i+6]            };
   temp = append(SevenStrings,mystring);
   println(SevenStrings);
   saveStrings("writesomefile.txt", temp);
 }  
}



                                    Any ideas appreciated!!!
Re: txt word-wrap removal
Reply #1 - Aug 28th, 2009, 1:36am
 
What do you mean by "word-wrap removal"? Do you want to concatenate lines?

Code:
String[] lines = loadStrings("text.txt");
String s = "";
int n = 3; // number of lines to concatenate
for (int i = 0; i < lines.length; i++) {
 s += lines[i];
 if ((i+1)%n == 0 || (i+1) == lines.length) {
   println(s);
   s = "";
 }
}
Re: txt word-wrap removal
Reply #2 - Aug 28th, 2009, 1:39am
 
Like many beginners, you make things harder and hard-code some stuff that the computer can do itself. If you have to change from 7 to 5, for example, you are in trouble...
Here is a simpler version of your code.
Code:
String lines[] = loadStrings("E:/tmp/loadsomefile.txt");
int joinBy = 7;

String[] neww = new String[lines.length / joinBy];
for (int i = 0; i < lines.length / joinBy ; i++)
{
 String currentString = "";
 for (int ii = 0; ii < joinBy; ii++)
 {
   // I would use a StringBuilder instead, but String is enough for this simple case
   currentString += lines[i * joinBy + ii] + " ";
 }
 neww[i] = currentString;
}
saveStrings("E:/tmp/writesomefile.txt", neww);

Note I have put saveStrings outside of the loop: in your version, each loop overwrite the previous content of the file...
Re: txt word-wrap removal
Reply #3 - Aug 28th, 2009, 3:34am
 
  Thank you guys for help and quick response. That is what I need. I work with strings only couple weeks but within my loops that was really "hard case".
Re: txt word-wrap removal
Reply #4 - Aug 28th, 2009, 4:15am
 
Note: don't take bad my remarks about beginners... We all made the same errors, that's what make us to learn and improve.  Grin
Re: txt word-wrap removal
Reply #5 - Aug 28th, 2009, 4:54am
 
Smiley
Page Index Toggle Pages: 1