-
February 7th, 2011, 06:56 AM
#1
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");
}
-
February 7th, 2011, 08:05 AM
#2
Re: Searching for string in array
Originally Posted by smedens
... 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 [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
February 7th, 2011, 12:18 PM
#3
Re: Searching for string in array
Originally Posted by smedens
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|