Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2004, 06:59 PM
Registered User
 
Join Date: May 2004
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
 
Old July 8th, 2004, 04:12 AM
Authorized User
 
Join Date: Sep 2003
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).
 
Old July 8th, 2004, 06:20 AM
Registered User
 
Join Date: May 2004
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
 
Old July 9th, 2004, 02:34 PM
Authorized User
 
Join Date: Sep 2003
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 :)






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 03: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 02:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.