Results 1 to 3 of 3
  1. #1
    mary is offline Member
    Join Date
    Jul 2007
    Posts
    51
    Rep Power
    0

    Default problems with array index

    I have a vector
    I try to print out the smallest number After compiling I get this messege: Arrays index is out of bound Exception.
    what's the problem?
    Java Code:
    class VectorTest{
         public static void main(String[]args){
         	  int smallestvalue=Integer.MAX_VALUE;
         	  int lastvalue;
         	  int currentvalue;
         	  int []v={5,3,6,0};
         	  int i=0;
         	  while(i>=0){
         	         currentvalue= v[i+1];
                     lastvalue= v[ i ];
    
         	      if(current>last){
         	          smallestvalue=lastvalue;
         	        }else{
         	        smallestvalue=currentvalue;
         	     }
         	   i++;
             }	
                  System.out.println(smallest);
       }
    }

  2. #2
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    You are running off the end of the vector. Instead of a while loop try a for loop like so
    Java Code:
    for (int i = 0; I < v.length; i++)
      do your checking here;

  3. #3
    mary is offline Member
    Join Date
    Jul 2007
    Posts
    51
    Rep Power
    0

Similar Threads

  1. Help with Flesch Index
    By L_22 in forum Advanced Java
    Replies: 1
    Last Post: 03-31-2008, 06:30 PM
  2. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 07:33 AM
  3. z-Index problem
    By mjdousti in forum AWT / Swing
    Replies: 1
    Last Post: 12-29-2007, 02:34 AM
  4. Replies: 1
    Last Post: 12-14-2007, 08:25 AM
  5. Accessing index.dat files
    By vissu007 in forum Networking
    Replies: 1
    Last Post: 07-01-2007, 05:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •