Home > Software design >  Adding an if statement before a while loop gives wrong results
Adding an if statement before a while loop gives wrong results

Time:08-24

Why does adding an if statement before the while loop result in the first match being removed?

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class testRegex {
    public static void main(String[]args) {
        Pattern p = Pattern.compile("[a-zA-Z0-9] ");
        Matcher m = p.matcher("Name, 245 Street, City, Country, 101010");
        
        System.out.println(m.find());
        
        if(m.find()) {
            while(m.find()) {
                System.out.println(m.group());
            }
        } else {
            System.out.println("Nothing found");
        }
     }
}

The output without the if is correct.

Name
245
Street
City
Country
101010

Adding the if or anything before the while loop for that matter results in this:

245
Street
City
Country
101010

What am I doing wrong and how can I correct this? I'm doing this on Eclipse IDE.

CodePudding user response:

Here is an alternative approach using Java 8 streams:

final Pattern p = Pattern.compile("[a-zA-Z0-9] ");
Matcher m = p.matcher("Name, 245 Street, City, Country, 101010");
    
List<String> res = m.results().map(MatchResult::group)
                  .collect(Collectors.toList());
if (res.isEmpty()) {
    System.out.println("Nothing found");
} else {
    System.out.println(res);
}

Output:

[Name, 245, Street, City, Country, 101010]

CodePudding user response:

m.find() changes state, goes to the next match if present.

    //NO: System.out.println(m.find());
   
    if (m.find()) {
        System.out.println("Found:");
        do {
            System.out.println(m.group());
        } while (m.find());
    } else {
        System.out.println("Nothing found");
    }

CodePudding user response:

Why not use matcher.reset()?

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class testRegex {
    public static void main(String[]args) {
        Pattern p = Pattern.compile("[a-zA-Z0-9] ");
        Matcher m = p.matcher("Name, 245 Street, City, Country, 101010");
        
        System.out.println(m.find());
        m.reset();

        if(m.find()) {
            m.reset();
            while(m.find()) {
                System.out.println(m.group());
            }
        } else {
            System.out.println("Nothing found");
        }
     }
}

CodePudding user response:

Each call of "m.find" will move to the next match in within the search String. So you have to remember the Finding of the first call of m.find() or you use the while loop without the if statement. With a boolean flag you could keep track whether you found something or not to decide whether you want to print "Nothing found" afterwards.

Something like:

      boolean foundSomething = false;
           
      while(m.find()) {
        foundSomething = true;
        System.out.println(m.group());
      }
      if(!foundSomething){        
        System.out.println("Nothing found");
      }

CodePudding user response:

Don't use while alone because it is a fulfillment of the condition if it doesn't find what you are looking for it moves to the next use for or do-while

  • Related