This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
wrothmonk's profile picture

[error][java] java.lang.ArrayIndexOutOfBounsException

Started by wrothmonk, 04 January 2013 - 02:07 PM
wrothmonk #1
Posted 04 January 2013 - 03:07 PM
I'm attempting to make a loop that displays a password for a door on a hidden terminal.

My code:

function loop()
term.clear()
term.setCursorPos(1, 1)
print("The password is: luatekk")
loop()
end
loop()

When I run it this shows up on the terminal:

The password is: luatekk
java.lang.ArrayIndexOutOfBoundsException

My main goal is to display the password with ought letting people get access to the terminal for programming. If anyone can give me some code that will do that with ought errors that would be great but I would still like to know what is causing the error.
zekesonxx #2
Posted 04 January 2013 - 03:35 PM
You're calling the function from within itself. That's not allowed.
ChunLing #3
Posted 04 January 2013 - 04:07 PM
It's allowed, just not very smart.

What's happening is that your function clears the screen, prints that message, and then calls itself, and that version does the same thing (clears the screen, reprints the message, and then calls itself), and so on until you have nested the function call enough times to overflow the stack.

If you want to loop a function, then use a loop. Recursion is more for…well…things that actually need recursion, and aren't going to just infinitely recurse.
wrothmonk #4
Posted 04 January 2013 - 04:15 PM
Thanks for your help. I thought calling a function within itself at the end were the loops. I'll take another look a the the wiki.