简体   繁体   中英

Boolean value assignment in Java

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

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

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.

  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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM