how do I return the array value as an integer from the text file?

edited March 2018 in Questions about Code

can somebody tell me what I am doing wrong? I can't see it anymore. I think it is simple, but after staring at it for several hours I give up...

int geefCijfer (int[] cijfer1) {
  String [] cijfers =loadStrings("cijfers_klein.txt");
  int [] cijfer = new int [cijfers.length];
  for (int i = 0; i< cijfers.length; i++) {
    cijfer[i] = int(cijfers[i]);
    println(cijfer);
  } 
  return cijfer;
}

Answers

  • At the beginning

    you want int[] since you return an array

  • edited March 2018

    This next should work (untested):

    int[] geefCijfer (int[] cijfer1) {
      return int(loadStrings("cijfers_klein.txt"));
    }
    

    Kf

  • I doubt it ;-)

  • edited March 2018

    To get real help please post an entire code that’s runnable or a short version of it to demonstrate the problem

    And the first 10 lines of your file also formatted as code

  • void setup() {
      int[]  c1 =  geefCijfer();
      println("-------------");
      printArray(c1);
    }
    
    int[] geefCijfer () {
      String [] cijfers = {"1", "9", "8"}; // loadStrings("cijfers_klein.txt");
      int [] cijfer = new int [cijfers.length];
      for (int i = 0; i< cijfers.length; i++) {
        cijfer[i] = int(cijfers[i]);
      }
      printArray(cijfer);
      return cijfer;
    }
    
  • Answer ✓

    but kfrajer is right !!!

    void setup() {
      int[]  c1 =  geefCijfer();
      println("-------------");
      printArray(c1);
    }
    
    int[] geefCijfer () {
      String [] cijfers = {"1", "9", "8"}; // loadStrings("cijfers_klein.txt");
      printArray(cijfers);
      return int(cijfers);
    }
    
  • Thanks!!

Sign In or Register to comment.