Results 1 to 6 of 6
  1. #1
    viper110110 is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default [SOLVED] Search problem - array out of bounds

    Hey guys, I am trying to make a search for a 2D array. I am using a program called "Ready To Program Java" (school assignment). My 1D searches using my logic work exactly as planned, however my 2D does not.

    Java Code:
    public static String[] [] search (String searchFor, String word[] [], int length, int width)
        {
            boolean[] valid;
            valid = new boolean [length];
            int found = 0, j = 0;
    
            for (int i = 0 ; i < length ; i++)
            {
                valid [i] = false;
            }
            for (int i = 0 ; i < length ; i++)
            {
                //for (int k = 0 ; k < width ; k++)
                //{
                if (word [i] [0].compareTo (searchFor) == 0)
                {
                    valid [i] = true;
                    found++;
                    //k = width;
                }
                //}
            }
            
            String results[] [] = new String [found] [width];
            
    
            for (int i = 0 ; i < length - 1; i++)
            {
                //for (int k = 0 ; k < width ; k++)
                //{
                if (valid [i] = true)
                {
                    results [j] = word [i];
                    j++;
                }
                //}
            }
            return results;
        }
    it is returning the error

    java.lang.ArrayIndexOutOfBoundsException: 1
    at Search.search(Search.java:142)

    thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Use the array's own lengths to help you avoid going out of bounds:
    Java Code:
    public class Fubar
    {
    
      public static void main(String[] args)
      {
        int[][] my2dArray = 
        {
          {1, 2, 3},
          {5, 7, 2}, 
          {0, -20, 10},
          {1, 1, 1}
        };
        
        for (int i = 0; i < my2dArray.length; i++)
        {
          for (int j = 0; j < my2dArray[i].length; j++)
          {
            System.out.print(my2dArray[i][j] + ", ");
          }
          System.out.println();
        }
      }
    }

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Question...

    Why is there a "length-1" in your for cycle if your looping through the valid array?
    Java Code:
    for (int i = 0 ; i < length - 1; i++)
    ... besides, I agree with Fubarable's suggestion

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    viper110110 is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    solv'd

    but it wasn't any of the solutions above

    I had
    Java Code:
    if (valid [i] = true)
    which made valid true no matter what

    I now have
    Java Code:
    if (valid [i] == true)
    and now it works

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It's far better and cleaner just to write:
    Java Code:
    if (valid[i])

  6. #6
    viper110110 is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It's far better and cleaner just to write:
    Java Code:
    if (valid[i])
    thanks, I'll remember that one

Similar Threads

  1. Help. Binary Search Problem
    By Krooger in forum Advanced Java
    Replies: 1
    Last Post: 11-03-2008, 07:19 AM
  2. Newbie search array question
    By CirKuT in forum New To Java
    Replies: 19
    Last Post: 09-14-2008, 07:26 AM
  3. Problem with displaying search results from an array
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 04:34 AM
  4. Array Search Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:45 PM
  5. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 02:20 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
  •