• Post Reply Bookmark Topic Watch Topic
  • New Topic

Factorial Array  RSS feed

 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting an "ArrayIndexOutOfBounds" at line 7. Numbers within the range come out fine. Why is this happening? It is supposed to keep asking for a number until -1 is entered. I know there is no loop yet, just trying to solve the -1 problem.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Marco,

well, in line 6 and a half, put 'System.out.println(num);'
so you see what the actual value of num is. If that is out
of bounds, you know there is something wrong with
the 'validate' method.

Greetz,
Piet
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried it and it only applies to -1. The other numbers are fine
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-1 indicates that the program should be terminated, right? So that's what it should, instead of going through the remainder of the main method.
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct -1 should end the program. A while loop in the beginning of the main method like while(num != -1) { would work wouldn't it?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's pretty easy to test, isn't it? :-)
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I tried it different ways but it either doesn't print anything or prints infinitely
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't return an error but it's without a loop.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:Well I tried it different ways but it either doesn't print anything or prints infinitely

And I hate to sound like a broken record here, but it's because you're simply coding, rather than analysing WHAT you need to do before you code.

Also: that statement above isn't true, because you've since proved that when you use a for loop, it DOES work.
So what you need to do is work out why.

I will give you a little tip though. Lines 17-20 of your validate() method can be written a lot more simply, viz:
Winston
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to walk through your code and follow the instructions you gave just as the computer would execute them.

You asked how is it that num has a value of -1 and causes an exception on line 7. It seems like you think that the validate method somehow terminates execution of the entire program once -1 is entered by the user. It does not. Execution goes back to main and continues from where it left off. Again, try to follow the execution and do what you have instructed the computer to do, keeping track of variable values as they change. Then you should see why num is -1 when you get to line 7.
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finished it with about 1 minute before my lab started
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:I finished it with about 1 minute before my lab started

Well done!

Why not try for two next time? And maybe hours...

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!