• 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

Head first programme

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am working my way through the Head first Java book and can't work out why the following code won't compile, is it a typo in the book?



I am using Eclipse.

Many thanks

[Edit - added code tags - MB]
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TellTheDetails - what error do you get?

Edit - at second glance (now I've formatted the code) it looks like there's too many curly brackets. Look at line 27.
 
Rob Strange
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at Puzzle4.main(Puzzle4.java:22)


I have tried removing the curly bracket and it throws another error,

eclipse has no crosses in the code so I am at a loss.

thank you for your help.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not a compiler error - it's a run-time error. So we can probably assume that it does compile correctly. Sorry about misleading you with the {}s - the indentation isn't quite right, which threw me.

OK, now let's look at that exception. It says "ArrayIndexOutOfBoundsException" - which means you're trying to access an element of an array that doesn't exist. It tells you it's on line 22. I'm guessing that corresponds to line 21 of the code listed.

So, what's the value of x on that line? If you can't work it out, put a print statement on the line before to print the value. You set x to 6 on line 18, and then keep increasing it. So when it hits line 21 for the first time the value is 7 (also mentioned in the error message). But you've created obs with a length of 6 - obs[7] doesn't exist. Which throws the exception.

Actually, that whole loop looks suspect. You start at 6, and keep increasing x until it's no longer negative. Which means that without the exception it will never end (actually, in practice is will end once x gets too big to hold in an int variable, but that's probably not what you intended).
 
Rob Strange
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I will have another look and try to work it out, I think the biggest problem is I don't know exactly what the code is doing as you have just explained very clearly, many thanks.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That second while loop is really odd:


Arrays are tricky to handle, so go for maximum readability to avoid screwups. And avoid duplication. Try something like this:

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic