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 5 of 5

Thread: Java sequential search.

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java sequential search.

    I'm working on sequential search and this is my code to search a String. The problem I have is that once the array is found, it stop. I was wondering what if there are more than one occurrence of that String in the array. I'm trying to modify my code so it find all the occurence and print it. I thought of removing the "break" but then even if I remove it, the code would replace the "found" index by the next occurence that it find, so my program results in printing the last match that it find instead of all of them. Can anyone help me fix this? Thanks
    public static void findPerson(Assignment[] r, String toFind)
    	{
    	    int found = -1;
     
    	    for(int i = 0; i < r.length; i++)
    	       if (r[i].getPerson().compareTo(toFind) == 0)
    	           {
    	               found = i;
    	               break;
    	           }
            if (found != -1)
            {  // we have found the person
               System.out.println("We found " + toFind + " in the roster: ");
               System.out.println(r[found]);
            }
            else
               System.out.println(toFind + " is not in the roster");
    	}


  2. #2
    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: Java sequential search.

    Put the "found" indexes in an ArrayList.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java sequential search.

    I .add(found) onto the arrayList, is there a way to check if an ArrayList contains elements? Since the "found" only get added when the String match, if there no string match then there be nothing inside the list, so I want to check whether the list contains elements. Thanks

  4. #4
    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: Java sequential search.

    way to check if an ArrayList contains elements?
    The ArrayList class has lots of useful methods. Read the API doc to see what will work for you.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java sequential search.

    Found it. Thanks!

Similar Threads

  1. Replies: 4
    Last Post: April 13th, 2013, 02:40 AM
  2. Graph Search Theory with extend of BFS, UFS and A* Search in grid
    By keat84 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 7th, 2012, 09:46 AM
  3. Java search - help
    By cnilsson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 25th, 2011, 02:26 AM
  4. Reading from a sequential file to an array
    By Xrrak in forum File I/O & Other I/O Streams
    Replies: 15
    Last Post: August 20th, 2011, 01:33 PM
  5. Sequential Files!!!???
    By nitwit3 in forum Java Theory & Questions
    Replies: 7
    Last Post: July 27th, 2011, 08:22 AM