hi im getting index out of bound error i cannot find exactly were the error is coming from.can anybodyhelp.tnks

import java.util.*;

class allshortinVector
`{`
     static Vector<String> v2=new Vector();

    public static void main(String[] args)
    {
        Vector<String> v=new Vector();

        v.addElement("test");
        v.addElement("hello");
        v.addElement("JAVA");
        v.addElement("Program");



         for(int i=0 ; i < v.size() ; i++)
         {
             if( (v.elementAt(i).length()) < (v.elementAt(i + 1).length()) )  // checking which element is the longest
             {
                 v2.addElement(v.elementAt(i));  // store them in vector v2
             }
         }
        for(String z : v2)
        System.out.println(z);
    }
}

Recommended Answers

All 6 Replies

I bet its here v.elementAt(i + 1)

Greetings Colin_3! :)
You'll find with software development 10% is coding and 90% is debugging! ;) That being said, one way to debug the code - provided that the debugging tools aren't helping - would be to "go no tech". What do I mean? Simply this: grab a pencil and paper and (and the code you wish to debug) and trace through the program. Run through your loop step by step (yes, by hand) and see if you can locate the issue. Once you've found it, figure out a solution. :) Let us know if you get stuck.

Tekkno---

Tekkno: 90% debugging? no offence, but seems to me that if that is the case, you're writing some pretty crappy code.
debugging should be held to a minimal, somewhere closer to 0%, that is. of course, that is practically impossible, but if you have a decent analysis, and unit tests, you'll either have a lot less debugging, or you are doing something terribly wrong.

that being said: DaveAmour was right.

(v.elementAt(i + 1).length())

The values of i as index go from the very first possible index (0) to the very last (v.size() - 1).

So, when you call v.elementAt(i+1) during the last iteration, you are no longer in bounds (hence, the exception thrown) and trying to access a non-existing object in a non-valid index of v.

try changing your loop to:

for(int i=0 ; i < (v.size()-1) ; i++)

If you have an index out of bounds error then the error message will tell you the exact line where the error happened. No need to guess.

I have to disagree with Tekkno. Novices may go 10%/90%, but sucessful programmers go more like 40% design, 30% coding, 20% unit/integration testing, 10% debugging.
Also: Paper and pencil is an excellent way to design a program, but almost useless for debugging. Especially for beginners, most bugs come from a limited or incorrect understandig of Java (eg declaring a reference variable but not creating the object). No amount of thinking or paper will find such bugs. More important is to use the tools at hand - always print and read the complete Exception stack trace, and use lots of print statements to confirm the values of key variables during execution (or use your IDE's debugger, if you are using an IDE).

This:

if( (v.elementAt(i).length()) < (v.elementAt(i + 1).length()) )

is the trouble, since when you're inside the loop, i already has 1 added to it for the next time the loop runs. Since your vector holds a default of 10 values, on the last run through of your loop i + 1 results in 11, which is outside the bounds of the index of that vector.

You can add a new int j which serves the same function but upkeeps after the trouble line completes.

Well, I guess we all have our ways of debugging code. Paper and pencil has worked fine for ME (especially when I find the debugger "misleading" (i.e. - when it tells me the error is on say line 20 when in fact it's severl lines before because I forgot a semi-colon)).

Of course that's not to say that I would use it to go through 100 lines of code, but for a small section of code, yeah, it works. :)

Tekkno

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.