Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Array Search Method

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array Search Method

    Thanks for taking the time to look at this code. Basically instead of printing out the row that the searched surname is on it prints out line[i][2] i++. How Can i fix this. The Array Size is line[6][5]. And When i search the Furthest surname (line[0][5])
    it gives me an array out of bounds exception. Thanks you in advance for the help.
    public void searchSurname(String[][] line){
     
            boolean found = false;
            Scanner scan=new Scanner(System.in);
            String searchKey=scan.nextLine();
            int column = 0;
            for(int i=0; i<6; i++){
                    if(searchKey.equalsIgnoreCase(line[i][column])){
                            found = true;
                            column = i;
                    }
            }
            if(found==true){
                    System.out.println("Person is found!");
                    for(int i=0; i<line.length;i++){
                            System.out.println(line[i][column]);
                    }
            }else if(found==false){
                    System.out.println("Person is not found");
            }


  2. #2
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Array Search Method

    Can you please show the code where create and fill the 'String[][] line' array?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array Search Method

    it gives me an array out of bounds exception
    Please copy and paste the full text of the error message here.
    It gives the bad index value and the source code line number where the error occurred.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Array Search Method

    Quote Originally Posted by Kyuubi426 View Post
    The Array Size is line[6][5]. And When i search the Furthest surname (line[0][5])
    Remember that for an array of length N the last index is N-1.

Similar Threads

  1. Search for min and max values as columns position in array
    By susieferrari in forum Java Theory & Questions
    Replies: 3
    Last Post: April 28th, 2011, 07:39 AM
  2. Search for number in array and return index
    By Kevinius in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2011, 12:00 AM
  3. [SOLVED] Help with array of Strings...Linear Search?
    By Stockholm Syndrome in forum Collections and Generics
    Replies: 4
    Last Post: March 22nd, 2011, 03:31 PM
  4. [SOLVED] Couldn't search for a string in an array.. Help please..
    By astrojunk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 10:47 PM
  5. Trouble with Binary Search on Insert Method
    By EricSt in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 17th, 2010, 10:34 PM