Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 7th, 2004, 06:59 PM
Registered User
 
Join Date: May 2004
Location: , NY, USA.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to DRAYKKO Send a message via Yahoo to DRAYKKO
Default Chapter 4, Exercise 3

NOTE: This is from JDK 1.3 Edition

I'm getting an ArrayIndexOutOfBounds exception (at runtime) for the following piece of code:

for(int x = 0; x < dragonTextArray.length; x++) //REM; tells the loop
                                                        // when to stop
        {
              if(dragonTextArray[x].compareTo(dragonTextArray[x+1]) < 0)
                {
                    storage = dragonTextArray[x];
                    dragonTextArray[x] = dragonTextArray[x+1];
                    dragonTextArray[x+1] = storage;
                }

                //NOTE: if previous string is less than next string,
                // or previous tring is equal to next string
                // leave them alone
          }

According to the diagnostics the offending code is:

      dragonTextArray[x] = dragonTextArray[x+1];

and I'm not sure how to fix this. I thought the statement "x < dragonArray.length" should take care of the loop extending beyond the last element in the array. Can someone give me some insite as to what's happening here? Thanks.

Dre---

That which does not kill me makes me stronger
  #2 (permalink)  
Old July 8th, 2004, 04:12 AM
Authorized User
 
Join Date: Sep 2003
Location: den Haag, , Netherlands.
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by DRAYKKO

I'm getting an ArrayIndexOutOfBounds exception (at runtime) for the following piece of code:

for(int x = 0; x < dragonTextArray.length; x++)
        {   
              if(dragonTextArray[x].compareTo(dragonTextArray[x+1]) < 0)
                {
                    storage = dragonTextArray[x];
                    dragonTextArray[x] = dragonTextArray[x+1];
                    dragonTextArray[x+1] = storage;  

According to the diagnostics the offending code is:

      dragonTextArray[x] = dragonTextArray[x+1];

and I'm not sure how to fix this. I thought the statement "x < dragonArray.length" should take care of the loop extending beyond the last element in the array. Can someone give me some insight as to what's happening here?
Ah, a question about a dragon text!! (first in months)

The ArrayIndexOutOfBoundsException is always thrown when you try to address an index of an array that does not exist.
So the question is: where exactly is that bug?
It IS in the for statement, but for a specific reason. Normally you would not call array[x+1], but in the for block you do, so I suggest:

for(int x = 0; x < dragonTextArray.length-1; x++)

alright?
:0)
Francis @ ch15

P.S. Bubble sorting, eh? Looks good. Only.. you are sorting the array from Z to A..
Also: BEWARE! The bubble sort isn't finished after going through the array elements once. You have to start over again until ALL are in place (all comparisons are < 0 in case you want to store your dragon names backwards).
  #3 (permalink)  
Old July 8th, 2004, 06:20 AM
Registered User
 
Join Date: May 2004
Location: , NY, USA.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to DRAYKKO Send a message via Yahoo to DRAYKKO
Default

Quote:
quote:Originally posted by freezotic


Ah, a question about a dragon text!! (first in months)

The ArrayIndexOutOfBoundsException is always thrown when you try to address an index of an array that does not exist.
So the question is: where exactly is that bug?
It IS in the for statement, but for a specific reason. Normally you would not call array[x+1], but in the for block you do, so I suggest:

for(int x = 0; x < dragonTextArray.length-1; x++)

alright?
:0)
Francis @ ch15

P.S. Bubble sorting, eh? Looks good. Only.. you are sorting the array from Z to A..
Also: BEWARE! The bubble sort isn't finished after going through the array elements once. You have to start over again until ALL are in place (all comparisons are < 0 in case you want to store your dragon names backwards).
Thank you Francis! I was approaching this issue from the idea of modifying the [x+1] index, but the solution kept eluding me.

Yes, you're correct this is a bubble sort (an exercise from the Beginning Java 2 (JDK 1.3 Edition) text). I'm glad you mentioned having to sort until all are in order. I had a feeling just doing it once wouldn't be enough; but the "specs" didn't mention that. :D

Thanks again for the help!

Dre---

That which does not kill me makes me stronger
  #4 (permalink)  
Old July 9th, 2004, 02:34 PM
Authorized User
 
Join Date: Sep 2003
Location: den Haag, , Netherlands.
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by DRAYKKO
Thanks again for the help!

Dre---
I am happy to have helped :)

 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 Exercise 2 diango BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 4 February 1st, 2011 02:24 PM
Chapter 3 - Exercise 3 AndyN BOOK: Beginning Cryptography with Java 3 August 16th, 2006 03:09 PM
Chapter 5 - Exercise 1 scgtman BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 May 16th, 2006 08:10 PM
Chapter 3 Exercise 3 Matt WAXON BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 July 4th, 2005 02:19 AM
Chapter 8, Exercise 4 cjo BOOK: Beginning ASP.NET 1.0 0 November 3rd, 2003 01:26 PM



All times are GMT -4. The time now is 03:43 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.