Display last sentence of inputted text.

HI folks,

I have a processing sketch which uses text inputted by the user via keyboard and then cycles through 3 screens (screen 0, 1 and 2). What I want is that when it returns to screen one, the last sentence of the previous user's text displays and the next user types after it (sort of like a chain story).

String myText = "Your story..."; //text that displays initially on Screen 0 String yourText = ""; // Variable to store text currently being typed String savedText = ""; // Variable to store saved text when RETURN is hit

Hitting RETURN advances the sketch to the next screen where text is manipulated and also saved out a .txt file

At the end of Screen 2, I want it to return to Screen 0 - but instead of displaying 'myText', I want it to display the last sentence of 'yourText' or 'savedText' (which are the same text).

I think I would have to search for the last occurrance of a period+space, then make yourText = whatever text appears after that period+space.

I know 'search' can locate the idex of the first instance of particular characters, but not sure how I would get it to locate the last instance. Is there a way I can get it to search the string from end to beginning, instead of beginning to end...or am I making this too complicated? Any help, gratefully accepted.

ON a separate note, since I changed the keyPressed to RETURN from CONTROL, all my characters are displaying twice. Any advice on that is welcome too.

Text input part of code below:

void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (yourText.length() > 0) {
      yourText = yourText.substring(0, yourText.length()-1);
    }
      } else if (keyCode == DELETE) {
        yourText = "";
      } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != RETURN && keyCode != ENTER && keyCode != ALT) {
        myText = "";
        yourText = yourText + key;
      }

  // If the Return key is pressed save the String and write it to text file
    //if (key == CODED) 
    //{
    if (key == RETURN )  {
      savedText = yourText;
      textFile = createWriter("stories/"+timestamp()+".txt");
      textFile.println(savedText);
      textFile.flush();
      textFile.close();
      time = millis();//store the current time
      rect (0,0,width, height); //PROBLEM sometimes visible when screen is switched.
      noStroke ();
      currentScreen++;
  if (currentScreen > 2) { currentScreen = 0; } //switches to next screen

  } 

 else {
      // Otherwise, concatenate the String
      // Each character typed by the user is added to the end of the String variable.
      yourText = yourText + key;
    }
  //}
}

Thanks.

Tagged:

Answers

  • I've already answered part of your question at your crosspost here, but it sounds like you just want an ArrayList of Strings to keep track of previous sentences.

Sign In or Register to comment.