String array loadStrings split

Please can anyone help me please? Iknow how to load Strings but idk how to Split them with “!,?,.”

Write a program that reads a text file with a name of your choice and has the following content:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua? At vero
eos et accusam et just duo dolores et ea rebum! Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor sit amet.

Your program should display this text sentence by sentence in the console. That means one sentence per line. As soon as a sentence ends, a new line should be started.

1 Like

I think you want splitTokens()

     String[] q = splitTokens(myLoadedStrings, "!?.");

see Reference / Processing.org

1 Like

Hello,

The Processing website has resources to help you:
https://processing.org/

Learning to navigate this site and use these resources is important and will help you progress.

For example:

:)

1 Like

Thank you for your answer. yes it worked but it deleted the separator`

String [] text = loadStrings("data.txt");
String [] finaleAusgabe = {};

for (int i =0; i<text.length; i++){
  finaleAusgabe = splitTokens(text[i], "!?.");
  printArray(finaleAusgabe);
}

`

AND after At vero no line break should occur!

1 Like

Hello,

Combine the strings and then split them comes to mind.

Give it some thought!

How can you make this work with what you have?

:)

1 Like

That’s true.

You can either

  • write your own function splittTokensPreservingSeparator that does what you need OR
  • you stay with your current approach: then you need to search each of the resulting sentences (in a for-loop) in the initial long text, check the separator in the initial long text (after the finding) and add separator to the resulting sentence.

Both are tricky. :wink:

1 Like

thank you for your reply)

2 Likes

too difficult to do it… still did not check it …

String [] text = loadStrings("data.txt");
String [] finaleAusgabe = {};
String [] c = {};
String [] a = {};
for (int i =0; i<text.length; i++){
  a = splitTokens(text[i], " ");
  c = concat(a,a);
  printArray(c);
  //finaleAusgabe = concat(finaleAusgabe, c);
  printArray(finaleAusgabe);
  //finaleAusgabe = concat(finaleAusgabe, c);
  //finaleAusgabe = splitTokens(text[i], "!?.");
  
}
//printArray(finaleAusgabe);

Hello, how can i combine the Strings?

I found the Solution!!! It was a long way!! thank you for your attention!!

String [] text = loadStrings("data.txt");
String [] finaleAusgabe = {};
String [] c = {};
String [] a = {};
for (int i =0; i<text.length; i++) {
  a = splitTokens(text[i], " ");
  finaleAusgabe = concat(finaleAusgabe, a);
}
for (int i=0; i<finaleAusgabe.length; i++) {
  print(finaleAusgabe[i]+ " ");
  if (finaleAusgabe[i].contains("!")) {
    print("\n");
  };
  if (finaleAusgabe[i].contains(".")) {
    print("\n");
  };
  if (finaleAusgabe[i].contains("?")) {
    print("\n");
  }
}
2 Likes

Well done!

Very proud of you right now!

Maybe I can show my way later. I guess yours is faster though…

Here is my version:


// String [] text = loadStrings("data.txt");
String [] text = {
  "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod", 
  "tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua? At vero", 
  "eos et accusam et just duo dolores et ea rebum! Stet clita kasd gubergren, no sea", 
  "takimata sanctus est Lorem ipsum dolor sit amet."
};

String [] finaleAusgabe = {""};

void setup() {
  //
  // wir setzen alle Zeilen zu einem String ergebnisSumme zusammen, weil sich ein Satz über mehrere Zeilen erstrecken kann
  String ergebnisSumme = ""; 
  for (int i = 0; i<text.length; i++) {
    ergebnisSumme += text[i].trim()+" ";
  }//for

  // letztes Leerzeichen entfernen  
  ergebnisSumme = ergebnisSumme.trim();
  println(ergebnisSumme);
  println( "***");   

  // Wir gehen den String ergebnisSumme buchstabenweise durch
  int indexFinaleAusgabe=0; 
  for (int i=0; i< ergebnisSumme.length(); i++) {

    // Wir hängen den Buchstaben an (oder auch "!", "?" oder ".")
    finaleAusgabe[indexFinaleAusgabe] += ergebnisSumme.charAt(i);

    // Satzende? 
    if ("!?.".contains(""+ergebnisSumme.charAt(i))) {
      // Ja
      // Die fertige Zeile wird noch beschnitten (betrifft ihren Anfang, an dem noch das Leerzeichen des letzten Satzes steht)
      finaleAusgabe[indexFinaleAusgabe] = finaleAusgabe[indexFinaleAusgabe].trim();

      // eine neue Zeile wird angehängt und der Index erhöht
      finaleAusgabe = append(finaleAusgabe, "");
      indexFinaleAusgabe++;
    }// if
  }//for

  // The last line of the array finaleAusgabe was probably empty so we remove it
  if (finaleAusgabe[finaleAusgabe.length-1].trim().equals("")) {
    finaleAusgabe=shorten(finaleAusgabe);
  }//if

  // Ausgabe des array finaleAusgabe
  for (String s1 : finaleAusgabe) { 
    println(s1);
  }
  println( "***");
}//func 
//


1 Like

Hello @nina1,

Pleased to see that you found your own solution.
I also explored many different ways to do this and your solution was unique and worked.

I am adding to this topic for future reference.

Examples here:

Going beyond the Processing functions you can also use the many Java methods available to you:

This reference links to the Java Documentation (Java 8) which has more string methods:
https://processing.org/reference/String.html

:)