2

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);
}
}
2
  • you should add an initial value to the boolean to eliminate the warning. boolean match = false; Also, you need to add break statement when the match is set. Commented Jan 28, 2013 at 7:14
  • 1
    @Drogba: Not can, have to ;) Commented Jan 28, 2013 at 7:15

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much! I learned a number of things from your edit!
3

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);

2 Comments

I think he's trying to list all of the matching files and only print No matches found for if there were no matches found.
You should clarify, that the counter has to be initialized outside the loop.
0
  1. You need to break the loop when you found your match.

  2. 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

Comments

0

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.

Comments

0

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;
    }
}

3 Comments

I think he's trying to list all of the matching files, which this won't do because of the break.
for this reason this snippet should be used: boolean match = files.lenght!=0; and in loop ` if(!filename.contains(searchString)) match=false;`
Nobody likes to read code in comments ;) You can always edit your answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.