1

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
}`
Liam Barry
  • 11
  • 1
  • 1
    *" ... turn an array of 10 ints into a formatted string phone number that looks like this: (999) 999-9999."* - You do not need Regex API. You can simply use [`String#join`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#join-java.lang.CharSequence-java.lang.CharSequence...-). – Arvind Kumar Avinash Jan 14 '23 at 20:25
  • `numbers.toString()` is not doing what you think it does. Try to print the result of it and you'll see what I mean. Apart from that, it would be good if you show how you call your methods and add the errors you get to your question. A simple "I keep getting errors" will not help anyone to help you. – Eritrean Jan 14 '23 at 20:35
  • `numbers.toString[]` will simply return `"[I@73a8dfcc"`. Also, `int space = 1` and `"%s" + space` will result in `"xxx1"`, not `"xxx "`. Seriously though, why apply a regex after the fact instead of formatting string directly? – knittl Jan 15 '23 at 10:56

1 Answers1

0

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)

knittl
  • 246,190
  • 53
  • 318
  • 364