CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2011
    Posts
    7

    Searching for string in array

    Got a problem searching for a string in an array. The searching works - i.e the string i search for is found, but how do I write the code for the string NOT being found, without recieving the same message every time j != country?

    search:
    for(int j = 0; j < names.length; j++) {
    if(names[j].equals(country)) {
    index = j;
    countries[index] = new Country(country, population);
    JOptionPane.showMessageDialog(null, "You changed the population in " + country + " to " + population);
    }
    else{

    //sys.out.print("Country not found, try again");
    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Searching for string in array

    Quote Originally Posted by smedens View Post
    ... how do I write the code for the string NOT being found, without recieving the same message every time j != country?
    Sorry, I don't understand what you're asking. It might help if you described what you want to happen rather than what you don't want to happen...

    A prudent question is one-half of wisdom...
    F. Bacon
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Searching for string in array

    Quote Originally Posted by smedens View Post
    how do I write the code for the string NOT being found
    You put that code after the for-loop.

    Inside the for-loop you just set a boolean variable indicating whether there was a hit or not.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured