My program keeps crashing; STRING (file) to INT

the first segment of code is the issue, without it, it works fine. I have tried int(string), Intiger.valueOf and Intiger.ParseInt. If you have any ideas pls tell me. (btw i saw no issue with the file, if you want i can link it, but its just random Slovenian words)

//Trying to find a word with different search methods, from a .txt document called "besede.txt"

//loading the number of word in the .txt from the first line of it.
String wLen = loadStrings("besede.txt")[0];
int intA = int(wLen); //trying to change String to Int, i'vs seen it work
  

int wnum = intA; //int(loadStrings("besede.txt")[0]); //number of words
String words[] = new String[intA];

int wl = 7; //word length //one of the search parimiters; length of the word

boolean searchBy[] = {true,false,false}; //1st - length, 2nd- letters, 3rd- letters known

String output[] = new String[wnum]; //output of all of the words, matching the criteria

int ol = 0; //output length

void setup() {
  
  size(100,100);  
  for(int i = 0; i < wnum; i++) { //loading all the words from .txt
    words[i] = loadStrings("besede.txt")[i+1];
  }
  //for(int i = 0; i < wnum; i++) {
  //  println(words[i]);
  //}
}
void draw() {
  //first criteria; length test
  for(int i = 0; i < wnum; i++) {
    if(words[i].length() == wl) {
      output[ol] = words[i];
      ol++;
    }
  }
  finish();
}

void finish() {
  for(int i = 0; i < ol; i++) {
    println(output[i]);
  }
  exit();
}
1 Like

Please study reference for loadStrings

Also study tutorial for array

2 Likes

There a re few things you can do yourself before asking for help.

First, you can isolate the problem: Trying running the culprit lines in a sketch by themselves.

Second: Use print() to see what the values you are converting

Third: Always check you dont have a null case

Fourth: Base on the error, do a search online and in the forum

If the issue persist, share your code and tell us what you tried.

Now, I have to add that you are doing something you suppose not to do. In the code you provided above, you are adding code outside setup and draw functions. For instance, the proper way to do it:


String wLen ;
int intA ;

void setup(){
  size(100,100);  
  String wLen = loadStrings("besede.txt")[0];
  int intA = int(wLen); //trying to change String to Int, i'vs seen it work

  ... //Rest of the code

Please check the second bullet in the Troubleshooting guide under the section: Common Issues (That Are Not Bugs).

Kf

1 Like