Array out of bounds


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Array out of bounds

  1. #1
    Join Date
    Dec 2005
    Posts
    27

    Array out of bounds

    Can anyone tell me why i am getting a java.lang.ArrayIndexOutOfBoundsException: 4
    at Piglatinator.calculateVowelPositions(Piglatinator.java:53)
    What this method is trying to do is calulate the vowel positions of a string entered by a user.

    Code:
        public static int calculateVowelPositions(String str) throws ArrayIndexOutOfBoundsException 
    
        {
            str = str.toLowerCase(); 
            str = str.trim(); 
            int vowelPositions [] = new int [4]; 
            {
                vowelPositions [0] = str.indexOf('a');
                vowelPositions [1] = str.indexOf('e');
                vowelPositions [2] = str.indexOf('i');
                vowelPositions [3] = str.indexOf('o');
                vowelPositions [4] = str.indexOf('u');
            }   
            int position = 0; 
            for(int i = 0; i < vowelPositions.length + 1; i ++)
            {
                position = Math.min((int) vowelPositions[i -1], (int) vowelPositions [i]);
            }
            return position; 
        }

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    The size of the array doesn't mean 0-4, it means it has a size of 4 (0, 1, 2, 3, the first 4 numbers, starting at 0). If you have an array with size 7, it would be 0, 1, 2, 3, 4, 5, 6.

    an easier way to do what you're trying to do would be:
    Code:
    char vowelPositions [] = { 'a', 'e', 'i', 'o', 'u' };
    which does the same thing that you have up there.

  3. #3
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Code:
    for(int i = 0; i < vowelPositions.length + 1; i ++)
    {
        position = Math.min((int) vowelPositions[i -1], (int) vowelPositions [i]);
    }
    You're looping way too many times, which will result in more ArrayIndexOutOfBoundsExceptions.

    Your for loop is doing the same thing as this:
    Code:
    // ArrayIndexOutOfBoundsException: -1
    position = Math.min(vowelPositions[-1], vowelPositions[0]);
    position = Math.min(vowelPositions[0], vowelPositions[1]);
    position = Math.min(vowelPositions[1], vowelPositions[2]);
    position = Math.min(vowelPositions[2], vowelPositions[3]);
    // ArrayIndexOutOfBoundsException: 4
    position = Math.min(vowelPositions[3], vowelPositions[4]);
    I have no idea how this method relates to what you're trying to do though...

    Here's a method that will return an array containing 1s and 0s, 1s meaning a vowel is in that position and 0 meaning not a vowel. for example, apple would be: 1 0 0 0 1
    Code:
    public static int[] vowelPositions(String str) {
        /** will store the positions of vowels in str */
        int[] positions = new int[str.length()];
    	
        for (int i = 0; i < positions.length; i++) {
            switch(str.toLowerCase().charAt(i)) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                positions[i] = 1;
                break;
            default:
                positions[i] = 0;
                break;
            }
        }
        return positions; 
    }
    I haven't compiled the code above yet, but it should work.
    Last edited by destin; 01-02-2006 at 10:55 AM.

Similar Threads

  1. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 09:09 AM
  2. redim help !!!
    By angelito in forum VB Classic
    Replies: 1
    Last Post: 11-21-2005, 06:16 AM
  3. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 09:05 AM
  4. Index Outside the Bounds of the Array
    By Jason Salas in forum ASP.NET
    Replies: 2
    Last Post: 04-24-2003, 08:25 PM
  5. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 06:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center