-2

I'm new to java. why this don't work ?? this must get something like this formatPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) and after that return (123) 456-7890

Thanks for the answers.

public class Program {                                                                             
    public static String formatPhoneNumber(int[] nums) {                                               
        String nums2=nums.toString();
        String a_final ="";
        //"(123) 456-7890"
        a_final="\""+("+nums2.substring(0,2)+")"+"\t"+nums2.substring(3,5)+"-"+nums2.substring(6,9)+"\"";
        return a_final; 
                                                                                                 
    }
}

S.Roshanth
  • 1,499
  • 3
  • 25
  • 36
M.K
  • 1
  • 3

5 Answers5

3

When using the toString(); method in a primitive type array the result is the location in the memory e.x "[I@1b28cdfa"

If you want to copy your array of numbers in a String just use the StringBuilder

StringBuilder builder = new StringBuilder();

    for (int number : nums){

        builder.append(number);

    }

    String nums2 = builder.toString();

    
    String a_final="("+nums2.substring(0,3)+")"+nums2.substring(3,6)+"-"+nums2.substring(6,9);


    System.out.println(a_final);
  • Are you sure that your code produces the expected output (as posted in the question)? Looks to me like the last call to method `substring` has the wrong arguments. – Abra Jul 15 '21 at 10:48
0

String nums2=nums.toString();

doesn't do what you think it does. Check the result by printing it with System.out.println A quick way to make it do what you think it does is String nums2=java.util.Arrays.toString(nums).replaceAll("\\D", "");

g00se
  • 3,207
  • 2
  • 5
  • 9
0

You can use MaskFormatter

import javax.swing.text.MaskFormatter;
//...
public static String formatPhoneNumber(int[] nums) throws ParseException {
    StringBuilder sb=new StringBuilder();
    for(int value:nums){
        sb.append(value);
    }
    String phoneMask= "(###)-###-####";
    MaskFormatter maskFormatter= new MaskFormatter(phoneMask);
    maskFormatter.setValueContainsLiteralCharacters(false);
    return maskFormatter.valueToString(sb.toString()) ;
}
yuri777
  • 377
  • 3
  • 7
0

Here is an alternative way to convert an int array to a String using the stream API.
(Notes after the code.)

import java.util.Arrays;
import java.util.stream.Collectors;

public class Program {
    public static String formatPhoneNumber(int[] nums) {
        String nums2 = Arrays.stream(nums)
                             .mapToObj(i -> String.valueOf(i))
                             .collect(Collectors.joining());
        String aFinal = String.format("\"(%s)\t%s-%s\"",
                                      nums2.substring(0, 3),
                                      nums2.substring(3, 6),
                                      nums2.substring(6));
        return aFinal;
    }

    public static void main(String[] args) {
        System.out.println(formatPhoneNumber(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}));
    }
}

Since final cannot be used as a variable name, you changed it to a_final. I think you should try to adhere to java naming conventions and hence I changed the variable name to aFinal. Using conventions makes it easier for other people to read and understand your code which helps when you want people to help you fix the problems in your code.

Also, the indexes you use in the calls to substring do not match your expected output (as posted in your question). In the above code, I corrected those also.

This is the output I get when I run the above code.

"(123)  456-7890"
Abra
  • 19,142
  • 7
  • 29
  • 41
-1

The toString method for an int[] returns:

getClass().getName() + "@" + Integer.toHexString(hashCode());

Hence, you won't get all the numbers simply appended as string. To get the nums2 string, you can just append the nums elements one by one as:

String nums2 = "";
for (int e : nums)
    nums2 += e;

And the final result string would be:

a_final="\""+"("+nums2.substring(0,3)+")"+"\t"+nums2.substring(3,6)+"-"+nums2.substring(6,9)+"\"";

Please note your a_final wasn't correct as it was missing some of the characters. I've modified it in the above.

GURU Shreyansh
  • 881
  • 1
  • 7
  • 19