1

package files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {
 public static String arr[];
 public static void main(String[] args) throws Exception {
  
  String fiveFour="kkkkaaaaa";
  String condition="[a]{5}|[k]{1}";
  int size=0;
  regTest(condition,fiveFour,size);
 System.out.println(size);
  
  
  
 }
 
 public static int regTest(String regexThing, String toCheck, int size){
  Pattern pattern= Pattern.compile(regexThing);
  Matcher regexMatcher= pattern.matcher(toCheck);
  
  int i=0;
  
  while(regexMatcher.find()){
   i++;
   System.out.println(regexMatcher.group());
   if(regexMatcher.group().length()!=0){
    System.out.println(regexMatcher.group().length());
    
    
    
   }
   
   
  }
  
  return i;
  
 }
 // work on return try maybe a increment and returning that at the end!
 //stacks
 
}

I'm having trouble returning my regexMatcher.group().length(), in my sop it prints out 5 which is correct. However it won't assign that value to size so I can return it. Any help? It's a logic error and I don't see where I went wrong.

package files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {
    public static String arr[];
    public static void main(String[] args) throws Exception {
        String fiveFour="kkkkaaaaa";
        String condition="[a]{5}|[k]{1}";
        int size=0;
        regTest(condition,fiveFour,size);
        System.out.println(size);
    }

    public static int regTest(String regexThing, String toCheck, int size){
        Pattern pattern= Pattern.compile(regexThing);
        Matcher regexMatcher= pattern.matcher(toCheck);

        int i=0;

        while(regexMatcher.find()){
            i++;
            System.out.println(regexMatcher.group());
            if(regexMatcher.group().length()!=0){
                System.out.println(regexMatcher.group().length());
                i++;
            }
        }
        return size;
    }
    // work on return try maybe a increment and returning that at the end!
    //stacks
}
stack in
  • 13
  • 3

2 Answers2

0

You're never assigning the result of regexMatcher.group().length() to size.

So there are two things you need to change, the first is that inside your method, you want to add:

size = regexMatcher.group().length();

Now, inside the regTest method size has the value of 5 that you're expecting. BUT, because Java passes primitives by value (see this question for more explanation of what that means), this means that the size you pass in to the method is different from the size in the main method, so you have more to do.

You return the size from your method, so in the caller, you can assign that to the original size variable, like:

size = regTest(condition, fiveFour, size);

Now, because you're never actually using the size parameter in the regTest method, you don't actually need to pass it in, because you only actually care about it as the return value, so it might be cleaner to just have, in your main method:

int size = regTest(condition, fiveFour);

And then in the regTest method, get rid of size altogether and just have:

return regexMatcher.group().length();
DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
  • wow nice answer, im testing it right now. I always thought return changes the value passed in the method? But what your doing is assigning that vaiable with the method – stack in Oct 21 '17 at 07:47
  • what if I wanted to increment every found match , is there a way to do that? Thanks brotha – stack in Oct 21 '17 at 07:47
  • No, return effectively sends back a new value to the caller. If you don't assign the result of the method to anything, then the return is largely pointless, the value doesn't go anywhere. I'm not too sure what you mean about incrementing the find matches, there's a `groupCount()` method on the `Matcher` class, does that do what you want? If not, it would be best to ask a new question for that, to keep the answers separate. – DaveyDaveDave Oct 21 '17 at 08:12
  • I was facing a delima when I gave size the value of the length, and trying to return it wouldnt work? any input thanks – stack in Oct 21 '17 at 08:15
  • I set size =regexMatcher.group().length(); then used it in my return I get 0 everytime.. im not sure why? And well I was saying earlier in the while loop after every character found, I wanted to then set it to size and return it. But im facing that problem, where size does not return anything but zero, even when I set it properly. Could you show me a simple example please? – stack in Oct 21 '17 at 08:17
  • Ill post a example of what I ment – stack in Oct 21 '17 at 08:17
  • please take a look sir – stack in Oct 21 '17 at 08:18
  • Yep, this is what I'm saying in the second part of my answer, the `size` variable inside the `regTest` method, is not the same variable as it is in the `main` method. To get the value of `size` on the `main` method to be the value you're returning from `regTest`, you need to set it (in the `main` method), with `size = regTest(condition, fiveFour, size);`. The reason for this is a bit complicated, but the question I linked to in my answer explains it, or just search for 'Java pass-by-value', there are thousands of good explanations out there, that will all do a better job than me. – DaveyDaveDave Oct 21 '17 at 08:59
-1

The pattern ensures that the groups matching to the criteria are returned. No need to extra validation if(regexMatcher.group().length()!=0). primitive data types in java are immutable. If your intention is to find the total groups then the value i would give the same. Refer http://www.vogella.com/tutorials/JavaRegularExpressions/article.html