ERROR: The constructor "......" does not exist

Question [] questions; 
PImage [] bg;
int nr_img=10;
int counter=0;
void setup(){
  size(600,600);
  
  String [] text=loadStrings("questions.txt");//load the file
  questions=new Question[text.length]; 
  for(int i=0;i<questions.length;i++){
    String [] q=(split(text[i],","));
    questions[i]=new Question(q[i+1]);
  }
}
class Question{
  String [] options;
  int qnumber;
  int score;
Question(String [] options_){
  options=options_;
  
}
  void showQuestion(){
   for(int i=0;i<10;i++){
      text(options[i],300,300);
    }
  }

Hi everyone!
What I am trying to do here is use the questions from my text file which has 10 lines, so that I can show them on screen. I am using split to get a question per index using comma as a delimiter. I get the error that this constructor does not exist. While this is not close to done, it is what I have so far.
I hope the code is in the right format.
Any type of help would be greatly appreciated!
Thanks in advance!

Hello and thank you for responding!
I am using text.length so that the size of my array of Question objects is set according to the number of lines on my file.
The error does not appear when I use string instead of array of strings in the class attributes and constructor.
Is there a different approach that will not give me this error?

Hello,

Minimal changes to your code:

Question [] questions; 
PImage [] bg;
int nr_img=10;
int counter=0;
void setup()
  {
  size(600,600);
  
  String [] text=loadStrings("questions.txt"); //load the file
  questions=new Question[text.length]; 
  for(int i=0;i<questions.length;i++)
    {
    String [] q=(split(text[i],","));
    questions[i]=new Question(q); // Instantiate with an array! :)
    }

  questions[0].showQuestion(); 
  questions[1].showQuestion(); 
  questions[2].showQuestion(); 
  }


class Question{
  String [] options;
  int qnumber;
  int score;
Question(String [] options_)
  {
  options=options_;
  }
  
  void showQuestion()
    {
      for(int i=0;i<options.length;i++)
      {
      println(options[i]);
      }
    }
  }

I took at guess at format (not content) of your text file based on your code.

Text file:

who, what, where, why, when, how
0, 1, 2, 3, 4, 5
6, 7, 8, 9, 10

Output has leading spaces you can remove with trim()… see reference for this.

:)

2 Likes

Thank you so much it finally works!

1 Like