Searching for a string in a text file.

Full disclosure, I'm very new to programming and very green.

What I'm attempting to do is build a program that has a series of "code words" it searches for in a standard text file. If those words appear, some kind of function happens. Here's the basic layout.

Read a text file. Search the text file for the word "on". When "on" is found print a message. From this point forward in the text document, search for "off". When "off" is found print a message. Go back to searching for "on".

When end of the document is reached, I want the program to only search for new data and not keep rereading preexisting data. Essentially I want the program to "remember where it is" so if something updates the text document it doesn't start from the beginning again.

Here is the code I have so far. I've tested it a few times and have run into a problem. The entire file is searched for "on" before the program searches for "off." I want the program to search for "on" until an instance is found in the text file and then from that instance moving forward search for "off."

String codeword1 = "on";
String codeword2 = "off";
int mySwitch = 0;


void setup(){



}

void draw(){
  String [] myfile = loadStrings("C:/Steven/Twitch/Testing Arduino Communication/String Test.txt");
  if (mySwitch == 0){
  for(int i = 0; i < myfile.length; i++){
    String tobesearched = myfile[i];
    int index1 = tobesearched.indexOf(codeword1);
    if (index1 >= 0){
      println("Light will be on");
      delay(1000);
      mySwitch = 1;

    }
    else {
      println("String not found");
      delay(1000);
    }

}
  }
  if (mySwitch == 1){
    for(int i = 0; i < myfile.length; i++){
    String tobesearched = myfile[i];
    int index2 = tobesearched.indexOf(codeword2);
    if (index2 >= 0){
      println("Light will be off");
      delay(1000);
      mySwitch = 0;

    }
    else{
      println("I don't want this text to appear");
      delay(1000);
    }
    }
  }
}

The text file I used to test this program looked like this with each word on its own line in the text file:

Hello
There
on
RandomEntry
off

And the outputs of the programs were:

String not found.
String not found.
Light will be on.
String not found.
String not found.
I don't want this text to appear.
I don't want this text to appear.
I don't want this text to appear.
I don't want this text to appear.
Light will be off.

And then looped back to the beginning. Thanks in advance for any help.

Tagged:

Answers

  • edited April 2016 Answer ✓

    You need to have only 1 for loop, if you want to go through the file only once. if (mySwitch == 0)should be inside for loop, so as you find one word it would continue to search for another. I hope you got this logic:

    for(..){
    if (mySwitch == 0){
    // search for index1
    } else if (mySwitch == 1){
    // search for index2
    }
    

    Using delayby the way is not a good thing, as it makes entire program to sleep. You can read about proper timing in processing here: https://forum.processing.org/two/discussion/8084/how-do-i-display-a-message-for-a-few-seconds#latest

  • Thanks for the help Ater. I feel like I understand this a little better now. I made some changes to the code and added comments to reflect what each line is doing. I now have the following code.

    NOTE: I know I haven't fixed delay() yet, still looking through that thread you gave me.

    //Code words to be searched for
    String codeword1 = "on";
    String codeword2 = "off";
    
    //The switch allow program to go back and forth between searching for different codewords
    int mySwitch = 0;
    
    
    void setup(){
    
    
    
    }
    
    void draw(){
      //Load in the text file to be read, comes in as an array of strings
      String [] myfile = loadStrings("C:/Steven/Twitch/Testing Arduino Communication/String Test.txt");
        //Establishes which section of the array is being searched, then moves onto next section
        for(int i = 0; i < myfile.length; i++){
        if (mySwitch == 0){
        String tobesearched = myfile[i];     //Search each section of the array one after the other
        /*Gives -1 if codeword1 not found, else gives the char number for the location of codeword1,
        for example in the string "The light is on" function would give 14*/
        int index1 = tobesearched.indexOf(codeword1);
          if (index1 >= 0){
            println("Light will be on");  //Stand in for signal being sent to Arduino to turn on LED
            mySwitch = 1; //Starts looking for codeword2
            delay(1000); //Used so I can see the changes
    
        }
        else{  //If codeword1 is not found
           println("codeword1 not found");  //Prints message to show codeword1 is not found
           delay(1000); //Used so I can see the changes
        }
        }
        else if (mySwitch == 1){
          //Search each section of the array from the point codeword1 found
          String tobesearched = myfile[i]; 
          /*Gives -1 if codeword2 not found, else gives the char number for the location of codeword1,
          for example in the string "The light is off" function would give 14*/
          int index2 = tobesearched.indexOf(codeword2);
          if (index2 >= 0){
          println("Light will be off"); //Stand in for signal being sent to Arduino to turn on LED
          mySwitch = 0; //Starts looking for codeword1
          delay(1000); //Used so I can see the changes
        }
      else{ //If codeword2 is not found
           println("codeword2 not found"); //Prints message to show codeword2 is not found
           delay(1000); //Used so I can see the changes
        }
    }
      }
    }
    

    Now that I have the code working for strings that only have one value on each line I have a new question. The ultimate purpose of this code is to read text files that have more than one word on each line for these "code words."

    Is there a way to have the program look for "on" and then "off" in a single line of the array?

Sign In or Register to comment.