• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Factorial Array

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just tried it and it only applies to -1. The other numbers are fine
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This doesn't return an error but it's without a loop.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finished it with about 1 minute before my lab started
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic