Problem with loadStrings

Hi there, it seems to me, that the function loadStrings does not like to read String-variables. I have an array with filenames and want to tell loadStrings to load each file one after another. Like this:

for (int i = 0; i < filenames.length; i++) { texts[i] = loadStrings(filenames[i]); }

I would like to access and process many text files one after another. But how?

Thanks in advance!

Adrian

Answers

  • edited May 2017 Answer ✓

    https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    You can use join() in order to convert loadStrings()'s String[] to String:

    1. https://Processing.org/reference/loadStrings_.html
    2. https://Processing.org/reference/join_.html

    for (int i = 0; i < filenames.length; ++i) texts[i] = join(loadStrings(filenames[i]), ENTER);

  • edited May 2017 Answer ✓

    Or you can have texts declared as a 2D array instead: ;;)

    final String[] filenames = {
      "Pinocchio.txt", 
      "Shakespeare.txt"
    };
    
    final String[][] texts = new String[filenames.length][];
    
    for (int i = 0; i < texts.length; ++i)  texts[i] = loadStrings(filenames[i]);
    
    exit();
    
  • Thank you! I tried the 2D array, but must have failed in declaring it correctly, now that I see your code. On the other hand I would only get lost in dimensions, so I'd rather stick to the first solution join(). Thanks again! Cheers, A

  • But somehow it does not work...

  • @AdrainG -- re:

    I would like to access and process many text files one after another. But how?
    ...
    But somehow it does not work...

    You need to share a minimal example of what your code does to "process" your text files. Otherwise there is no way for forum members to guess what "it does not work" means -- we can't read your mind. Give a simple code examle; give the error message you are getting.

  • Hi Jeremy, sorry, you are right.

    My attempt was this: I have a file which contains a list of text files. So first I open the File-List, and then I use loadStrings in a for loop to open every single text. Let's put the case, that I want to open one after another and look what's on line 13, then save the line and store it into another textfile.

    I got this working so far:

    String[] files; String[] texts;

    files = loadStrings("fileList.lst"); printArray(files);

    texts = new String[files.length];

    for (int i = 0; i < files.length; i++) {

    println(files[i]); texts[i] = join(loadStrings(files[i]), ENTER);

    }

    Now how do I get to line 13 of every text, I have to split up every part of the texts-Array or I make the Array twodimensional, which is not working.

  • https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    • If you need to grab a specific line from a text, don't use join().
    • Just keep the String[] array received by loadStrings().
    • Then the 13th line would be index 12 from the String[] array. *-:)
  • It seems, as if loadStrings does only accept String-Parameters, so now this code works:

    String[] files;
    String[][] texts;
    
    files = loadStrings("fileList.lst");
    texts = new String[files.length][];
    
    for (int i = 0; i < files.length; i++) {
    
      String temp = files[i];
      texts[i] = loadStrings(temp);
    
    }
    
    printArray(texts[1][13]);
    
Sign In or Register to comment.