hello im taking the course with Daniel Shiffman and have 1 question in regard to video 18.1

JaiJai
edited December 2015 in Questions about Code

good days fellas, im wondering why when this array gets to the ewnd it stops ? i would like if it kep looping, now i try to plade the index fork in the constructos of the void loop and i try to run the draw function in the loop in the bracket section and still cant im not sure what is it i should do i try two things i kinda know about, a little help would be big.

What im doing is getting the window to keep repeating it self or continue to keep drawing in the window not in the processing window but in the drawing window, and in the processing windows im going to get a couple of the same print but diff styles one in the array the other in lines and finally each word being print out individually with the ""

String[] words;
String[] lines;
int index = 0;
String konstructor;

void setup() {
  size(600, 400);
  background(0);
  lines = loadStrings("Celebro.txt");
  konstructor = join(lines, " ");//in between ",./;']{\|-=+~`@!^%&" delimeter is place 
  printArray(lines);
  println(konstructor);
  words = splitTokens(konstructor);//in between ",./;']{\|-=+~`@!^%&" delimeter is place
  frameRate(5);//-slow,+fast
}

void draw()
{
  background(0);
  fill(255);
  textSize(64);
  textAlign(CENTER);
  text(words[index], width/2, height/2);//words[index].toLowerCase(),
  index++;
  println(words[index]);//  printArray(words[index]);
}
Tagged:

Answers

  • Answer ✓

    index = (index + 1) % words.length;

  • im getting this ArrayIndexOutOfBoundException:18 ? what doe it mean Gotoloop? and thanks for the pointer but the issue with that is that it will repeat the window good ! but also in the processing debugging window and that where ill like to be repeated only once and not in a loop, because before you helped me it was getting to the end of the txt document and the window would freeze and not let me X out i can only Stop it from the processing window,

  • _vk_vk
    edited December 2015

    It is not clear what you want, nor what your problem is. Perhaps you would like some basics?

    https://forum.processing.org/two/discussion/8093/technical-faq#latest

    https://processing.org/tutorials/

    Anyway.

    String[] name;
    

    is an array. Arrays are like a drawer that stores several different units of the same stuff. Each drawer is accessed by a number id. First is 0

    so

    name[4];
    

    is accessing the drawer number 5 (starts with zero)

    draw() is a loop executed 60 times each second. In your draw() you increment index (index++) so each "frame" ( a complete draw cicle ) you get the next drawer, or the String stored in the next drawer..

    What if you "ask" for a drawer that does not exist? Error. Which Error? ArrayIndexOutOfBoundException. Meaning you tried to access a draw that was not created.

    Lets' step back.

    To make an array you need to declare, initialize it, then you can populate it and access stuff stored there...

    //declare
    String anArray;
    
    // initialize
    anArray = new String [numberOfSlots];// setting the length of the array
    
    // populate
    
    anArray[0] = "zero";
    anArray[1] = "one";
    

    and so on. you can also do it in some other ways, see FAQ about arrays.

    Once you create an array, it has a limited immutable (that's not true, but for now let's assume it is) length. This length is determined by numberOfSlots, and can be accessed like anArray.length.

    So you want to stay in boundaries. But you got nothing preventing index to go over the length of your array. @GoToLoop used the modulo operator (look in reference) to keep index inside desired boundaries.

    try this

    int index = 0;
    void draw(){
    println(index);
    index = (index + 1) % 20;
    }
    

    see?

    If you need index to behave in a different way, you have to work this. As we can't really understand the desired behaviour, we can't help much.

    It's very common to see arrays accessed inside a for loop (reference). It looks some like:

    for (int i = 0; i < anArray.length; i++){
        anArray[i] = "something" + i;
    }
    

    You are not doing this, and that's ok, just to mention as you will see this a lot.

    Well you'd be better served by reading the stuff I pointed you than my shallow and inexact explanation.

  • JaiJai
    edited December 2015

    @_vk you must of not understood my title, as it does say im in a basic tutorial by me taking about Daniel Shiffman videos and im also letting you know what video by referencing to that video 18.1, i know about array and i know what the error i got means but what i dont see is why im getting it when im not asking processing to throw a fit when it gets to the end of the array list simply just loop since thats what draw is, if anything i dont understand really what % means or how is used but thats another subject i guess now my thing is that i want to display this in console one time

    [0] "#include <iostream>"
    [1] "#include <string>"
    [2] "using namespace std;"
    [3] ""
    [4] ""
    [5] "int main()"
    [6] "{"
    [7] ""
    [8] "  cout << "text";"
    [9] "  return 0;"
    [10] "}"
    #include <iostream> #include <string> using namespace std;   int main() {    cout << "text";   return 0; }
    

    also i would like just one time in console to print like this one time using println(Words[index]); / printArray(Words[index]);

    #include
    using
    std;
    main()
    cout
    "text";
    0;
    #include
    #include
    using
    std;
    main()
    cout
    "text";
    0;
    

    so that it looks like this

        [0] "#include <iostream>"
    [1] "#include <string>"
    [2] "using namespace std;"
    [3] ""
    [4] ""
    [5] "int main()"
    [6] "{"
    [7] ""
    [8] "  cout << "text";"
    [9] "  return 0;"
    [10] "}"
    #include <iostream> #include <string> using namespace std;   int main() {       cout << "text";   return 0; }
    #include //individually one  at a time
    using //individually one  at a time
    std; //individually one  at a time
    main() //individually one  at a time
    cout //individually one  at a time
    "text"; //individually one  at a time
    0; //individually one  at a time
    

    as in right now the program is doing what i want except for the part that it keeps printing on the console println(Words[index]); individually one at a time as it gets called but in a loop the only thing i wan in a loop is the draw words on center of screen

      background(0);
      fill(255);
      textSize(100);
      textAlign(CENTER);
      text(Words[index], width/2, height/2);//Words[index].toLowerCase(),
      index++;
      index = (index + 1) % Words.length;//i can remove this to stop at end of token
    
  • edited December 2015
    // forum.Processing.org/two/discussion/13900/
    // hello-im-taking-the-course-with-daniel-shiffman
    // -and-have-1-question-in-regard-to-video-18-1
    
    // GoToLoop (2015-Dec-11)
    
    static final String[] STATEMENTS = {
      "#include <iostream>", 
      "#include <string>", 
      "using namespace std;", 
      "", 
      "", 
      "int main()", 
      "{", 
      "", 
      "  cout << \"text\";", 
      "  return 0;", 
      "}"
    };
    
    void setup() {
      frameRate(1);
    }
    
    void draw() {
      background((color) random(#000000));
      println(STATEMENTS[frameCount - 1]);
      if (frameCount == STATEMENTS.length)  exit();
    }
    
  • @GoToLoop thats nice thanks but not what i meant as in the txt file is already has the txt in it i just want to display that like so, place all of this in a .txt file so that you can do the ame in both the draw window and console window but in console write just onces and in the draw window keep looping rather then the colors you had

    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    
      cout << "text";
      return 0;
    }
    
  • ... but in console write just once...

    void setup() {
      size(600, 200);
      smooth(4);
      frameRate(.5);
    
      textAlign(CENTER, BASELINE);
      textSize(050);
      fill(#FFFF00);
    
      printArray(STATEMENTS);
    }
    

    ... and in the draw window keep looping...

    void draw() {
      background(0100);
      int idx = (frameCount - 1) % STATEMENTS.length;
      text(STATEMENTS[idx], width>>1, height>>1);
      frame.setTitle(str(idx));
    }
    
  • @GoToLoop i did this which is more of what im looking for

    String[] Lines;
    
    
    void setup() {
      size(600, 200);
      smooth(4);
      frameRate(1);
      Lines = loadStrings("Celebro.txt");
      textAlign(CENTER, BASELINE);
      textSize(050);
    
      printArray(Lines);
    }
    
    void draw() {
      background(0100);
      int idx = (frameCount - 1) % Lines.length;
      text(Lines[idx], width>>1, height>>1);
      //frame.setTitle(str(idx));//name your title bar in window
    }
    

    but this is what more of what i want to do just once

    println(Words[index]);
    #include
    using
    std;
    main()
    cout
    0;
    
  • edited December 2015

    solved?

    I don't understand this:

    but this is what more of what i want to do just once

    but you can for-loop over your array in setup and say

    for (i............. 
          result = result + Lines [i] + "\n";
    

    and in draw:

    void draw() {
      background(0100);
      text(result, width>>1, height>>1);
    }
    

    and before setup()

    String result = "";

Sign In or Register to comment.