Liam Barry
Liam Barry

Reputation: 11

How do I fix my capturing groups to capture parts of a string of numbers?

As a new programmer, I am working on a problem that tells me to turn an array of 10 ints into a formatted string phone number that looks like this: (999) 999-9999. I am getting an error/issue with Matcher.

I tried to turn the array of ints into a single string, and then I tried to create a matcher and capturing groups to split the number into the first 3 numbers, the second three, and the last four. I keep getting errors and I do not know what exactly is going wrong. This is what my code looks like:

`import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;

public class Kata {
  
  private final static String pattern = "(?<first>[0-9][0-9][0-9])+(?<second>[0-9][0-9][0-9])+(?<third>[0-9][0-9][0-9][0-9])";
  private final static Pattern paddy = Pattern.compile(pattern); 
  
  public static Matcher match(String text){
        var mitch = paddy.matcher(text);
        mitch.find();
        return mitch;
  }//end method
  
  public static String createPhoneNumber(int[] numbers) {
    int space = 1;
    return String.format("(%s)" + space + "%s-%s", partOne(numbers), partTwo(numbers), partThree(numbers));  
  }//end method
  
  public static String partOne(int[] numbers){
    String phoneNumber = numbers.toString();
    return match(phoneNumber).group("first").strip();
  }//end method
    public static String partTwo(int[] numbers){
    String phoneNumber = numbers.toString();
    return match(phoneNumber).group("second").strip();
  }//end method
    public static String partThree(int[] numbers){
    String phoneNumber = numbers.toString();
    return match(phoneNumber).group("third").strip();
  }//end method
}`

Upvotes: 1

Views: 49

Answers (1)

knittl
knittl

Reputation: 265181

Why do you need a regular expression at all? You already have all the digits, now you need to put them into your string. Which ingredients do you need?

  1. A method which accepts an array of int, an offset, and a length. It will return these ints concatenated into a single string
  2. A String.format call which adds the parentheses and the hyphen and uses the %s format specifier to insert your number parts.

The method could look like this:

String formatDigits(final int[] digits, final int offset, final int length) {
  final StringBuilder sb = new StringBuilder();
  for (int i = offset; i < offset + length; ++i) {
    sb.append(digits[i]);
  }
  return sb.toString();
}

And then your string format call:

String.format(
        "(%s) %s-%s",
        formatDigits(numbers, 0, 3),
        formatDigits(numbers, 3, …),
        …);

If providing numbers to every call feels repetitive, you are correct. To avoid that, you could introduce a class PhoneNumber which expects the int[] numbers array in its constructor to store it in a field. It could then expose a method formatPhoneNumber() which performs the formatted and returns the formatted string.

Mandatory XKCD: https://xkcd.com/1171/ (and Jeff Attwood's blog: https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/, albeit less relevant to this question)

Upvotes: 0

Related Questions