2

I want to write a java API which excepts two input parameters. First inputStr and second strFormat.

public String covertString(String inputStr, String strFormat)
{
  // Need logic
}

For example,

Input Arguments- inputStr: 999999999, strFormat: xxx-xx-xxxx
Output :  999-99-9999

Input Arguments- inputStr: 1112223333, strFormat: (xxx) xxx-xxxx
Output :  (111) 222-3333

Please suggest if there are any utilities available? If not, best way to implement this problem?

6
  • 3
    what did you try yourself? i'd generally advise using regular expressions or as an easy option substring functionality of java's string class. This site is not about getting the right solution for free it's about getting help when you're stuck with a problem. i don't see where exactly you're stuck here :-) Commented Mar 12, 2014 at 12:50
  • Can you paste your code first Commented Mar 12, 2014 at 12:50
  • And how do you handle strings which are too short/too long/don't contain numeric chars? Commented Mar 12, 2014 at 12:52
  • Thanks for the quick response. I was trying to solve this in a traditional way like reading strFormat characters 1 by 1, if I find "x", I would write digit from inputStr, else I write the character or white space. But I am just confirming if there is any utility available to address this problem. Commented Mar 12, 2014 at 12:58
  • Well, you could use a Formatter, too. Commented Mar 12, 2014 at 13:00

6 Answers 6

3

Try this:-

import javax.swing.text.MaskFormatter;


String inputStr="11122288988";
String strFormat="(###) ###-#####";
public String covertString(String inputStr, String strFormat)
{
    MaskFormatter maskFormatter= new MaskFormatter(strFormat);
    maskFormatter.setValueContainsLiteralCharacters(false);
    String finaldata=maskFormatter.valueToString(inputStr) ;
    return finaldata;
}

Output:-

Input data :- 11122288988
Formatted Data :- (111) 222-88988
Sign up to request clarification or add additional context in comments.

1 Comment

This looks great. Let me try. Thanks for your response.
2

you go through strFormat characters 1 by 1, if you meet "x", you write digit from inputStr, else you write the character

Comments

1

This should work, but assumes that the input string is the correct length etc. If such checks are to be implemented, are left to the OP to implement:

public String covertString(String inputStr, String strFormat)
{
    final char[] array = strFormat.toCharArray(); // dups the content
    int inputIndex = 0;

    for (int index = 0; index < array.length; index++)
        if (array[index] == 'x')
            array[index] = inputStr.charAt(inputIndex++);

    return new String(array);
}

1 Comment

Thanks for the response. This is definitely one of the way to solve this kind of problem. I have also got other good/generic solution given by 'JDeveloper' (please see above answers).
0

the second parameter String strFormat you should implement the ASCII character of (-)and () in your logic .

I think this will help :http://javarevisited.blogspot.com/2012/08/how-to-format-string-in-java-printf.html

Comments

0

You can get your ouput with a specific pattern:

^(.{3})(.{3})(.{3})$ --> $1-$2-$3 to get 999-999-999 from 999999999
^(.{3})(.{3})(.{3})$ --> ($1) $2-$3 to get (999) 999-999 from 999999999

public String covertString(String inputStr, String strInputPattern, String strOutputPattern)
{
    //strInputPattern could be "^(.{3})(.{3})(.{3})$"
    //strOutputPattern could be "$1-$2-$3"
    return inputStr.replaceAll(strInputPattern, strOutputPattern)
}

I hope this help you

Comments

0

Use string.matches and filter your criteria with regex.

Like this -----

import java.util.Scanner;

public class SocialSecurityNumber {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    while (true) {
        System.out
                .println("Input Social security Number  (accepted form 123-45-6789): ");
        String s = input.nextLine();
        if (s.matches("\\d{3}-\\d{2}-\\d{4}")) {
            System.out.println("SSN --- valid.");
            break;
        } else
            System.out.println("SSN --- not valid.");
    }

    input.close();
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.