How to auto read and print the extra word from the updating text which can auto save

at first I load string in setup and because every time I need to loadstring ,so I write in draw function too, and code below, but there are the problem ,null pointerException.seems like sometimes there are null in the array ,but why ? actually I write something in the rtf, and even though maybe updating is the same text ,but there still will be something in the array .so could some guys help me with my problem or tell me if there are some better way to make the auto print extra word up

void setup()
{
  size(1020, 640);
  background(153,46,150);
  s=loadStrings("b.txt");
  totalS=join(s," ");
  words=splitTokens(totalS," ,.?!οΌŒγ€‚οΌŸοΌβ€œβ€γ€");
}
void draw(){
  if(frameCount%60==0)
  {
     currentS=loadStrings("b.txt");
     CurrenttotalS=join(currentS," ");
     currentWords=splitTokens(CurrenttotalS," ,.?!οΌŒγ€‚οΌŸοΌβ€œβ€γ€");
  }
}
void mouseReleased()
{
  if(CurrenttotalS.length()!=totalS.length()&&currentS!=null&&s!=null&&currentWords!=null&&words!=null)
  {
     for(int i=words.length;i<currentWords.length;i++)
    {
      println(currentWords[i]+i);
      println(currentWords.length);
    }
    words=currentWords;
  }
  else if(CurrenttotalS.length()==totalS.length())
  {
    println(0);
  }
}

Hi,

It would be great if you could provide a working example with the files you are using so we can try it home and help you debug it :slight_smile:

1 Like

As @jb4x said, we can’t run this without an example of the b.rtf file – or at the least a description of its contents.

Just to confirm – you do intend for this to be a primitive RTF parser? RTF is not plain text – the plaintext contents of an RTF file is full of markup, so you may get very surprising results. Here is an example of what you might be loading and parsing:

{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
 This is some {\b bold} text.\par
}

If you don’t care about RTF, then just save β€œb.txt” using plain text file format from Wordpad or TextEdit etc, then run your code on b.txt.

if on the other hand your goal is to count words in a bunch of RTF files then you should save your files as txt or use an RTF-to-TXT pre-processor, or use an RTF-parsing Java library.

1 Like

thank you for your reply . maybe i dont descripe well ,usually i use txt,it is not about the format. and for the file ,its just a nomal txt, i mean that you can create a empty txt,then run the sketch,and type some words and save , and the sketch can show the new word, again type some words,save .it can show the new word too ,in the meantime we dont run again and again the sketch.if you know whats wrong,tell me .

thank you for your reply . maybe i dont descripe well ,usually i use txt,it is not about the format. and for the file ,its just a nomal txt, i mean that you can create a empty txt,then run the sketch,and type some words and save , and the sketch can show the new word, again type some words,save .it can show the new word too ,in the meantime we dont run again and again the sketch.if you know whats wrong,tell me .

Notice you have a set of fields that you use once in setup and a set of fields that you use many times in draw. Instead of having this duplicated code using two different sets of fields, you could generate a single code and moved it to its own dedicated function:

void loadTXTdata(){
  s=loadStrings("b.txt"); 
  totalS=join(s," "); 
  words=splitTokens(totalS," ,.?!οΌŒγ€‚οΌŸοΌβ€œβ€γ€");
}

As describe before, if you are working with txt data, then use txt file extension as it is a convention that the extension describes the nature of your file. If you use rtf as the extension, it creates confusion. However, if you have a major reason to use rtf, then there is nothing you can do about it, right?

For autosaving, you need to define the trigger that will call your autosaving function (your autosave code). The trigger could be a time trigger, or a user event, or a frame count condition (exactly what you are doing in draw()).

For the null point exception (NPE), you need to provide the stacktrace of the error or tell us what line number the error report. Even better, what code is in that line number. Based on your code, the error would be in your mouse event.

Notice that you should check if the resources you are using are not null. When loading a file, Processing is nice to you and it will load the file for you. If there is an error, processing will handle it for you and it will return a null object, instead of showing a nasty error message with an error trace. You still need to due some work and ensure you can use the returned object. So the above function could be slightly modified to properly handle this case:

void loadTXTdata(){
  s=loadStrings("b.txt"); 
  if(s!=null){
    totalS=join(s," "); 
    words=splitTokens(totalS," ,.?!οΌŒγ€‚οΌŸοΌβ€œβ€γ€");
  }
  else{
    words=null;
  }
}

Where are your new words coming from? Is there an external code modifying the file and you are detecting the changes of this file? Or is there some other process in Processing? If so, can you provide more details?

Kf

1 Like