Reputation: 79
Apologies for the total noob question, but can anyone explain what's happening to the value of match
after the for-each loop has finished in the following method?
Attempts to compile produce the warning: variable match might not have been initialised
.
public void listMatching(String searchString) {
boolean match;
for(String filename : files) {
if(filename.contains(searchString)) {
System.out.println(filename);
match = true;
}
else {
match = false;
}
}
if(match == false) {
System.out.println("No matches found for " + searchString);
}
}
Upvotes: 2
Views: 29141
Reputation: 4596
It may files array is empty, so you should set default value for match variable;
boolean match=false;
for(String filename : files) {
if(filename.contains(searchString)) {
System.out.println(filename);
match = true;
break;
}
}
if you need to check that all of files has this search string U can use this code :
boolean match=files.lenght!=0;
for(String filename : files) {
if(!filename.contains(searchString)) {
System.out.println(filename);
match = false;
break;
}
}
Upvotes: 0
Reputation: 37813
Here's a fix that will do what you want it to:
public void listMatching(String searchString) {
boolean match = false; // initialize local variable
for(String filename : files) {
if(filename.contains(searchString)) {
System.out.println(filename);
match = true;
}
}
if(!match) { // same as 'match == false', just without comparison
System.out.println("No matches found for " + searchString);
}
}
Local variables have to be initialized. Only fields get the default value of their type.
If you reassign match
to false
in the else
block, it would be false
after the loop, even if every filename
contained searchString
except the last one.
Upvotes: 3
Reputation: 6121
Initialise the variable to false to avoid the warning in your program.
Also, match
is a single variable and depending upon the contents of various files (searching for the same string), you are assigning true or false to the same variable.
The final boolean value of match
is just the result of the search for the string in the last file of the list of files.
Upvotes: 0
Reputation: 15758
First you need to define boolean match = false;
Also,you need to break from the loop once you found the match , other-wise match
status will be over-ridden.
if(filename.contains(searchString)) {
System.out.println(filename);
match = true;
break;
} // this wil help whether a match is found or not
If you are interested in finding number of matches int counter = 0; if(filename.contains(searchString)) { System.out.println(filename); match = true; counter++; } // this wil help to find number of matches
finally System.out.println("number of matches for"+searchString+" : "+counter);
Upvotes: 3
Reputation: 7347
You need to break the loop when you found your match.
you need to initialize your found variable, you could also never run your loop, and then your if condition would look at a non initialized variable, thats what your compiler wants to tell you
Upvotes: 0