Search code examples
javastringformattinguser-input

Formatting user Input with specific char at specific point in java


*Edit 2,"undeclared variable" The following is the example that my textbook provides. I may have used incorrect terms in saying undeclared variable.

  • String fName = "Harry";
  • String lName = "Morgan";
  • String name = fName + lName;
  • results in the string "HarryMorgan"
  • What if you’d like the first and last name separated by a space? No problem:
  • String name = fName + " " + lName;
  • This statement concatenates three strings:
  • fName, the string literal " ", and lName.
  • The result is"Harry Morgan"

The citation provided is a guideline. Please check each citation for accuracy before use.

*Edit, Question is: How do I insert characters into an undeclared string?

Example:

Input: X839WS21R4E877
Display: X-839 WS21 R4-E87 (7)

Input: x83rws21b3e87a
Display: X-83R WS21 B3-E87 (A)

I am taking a 14 character input from the user. I need to format in spaces and " - " into the input. Also all of the letters need to be capitalized, although I have that already taken care of.

The following code is what I have.

System.out.println("Please enter your 14 character code.");
    String name = input.nextLine();

    if (name.length()>14) {//code thanks to namlik from stackoverflow 
        String cutName = name.substring(0, 14).toUpperCase();
        name = cutName;
    }

    System.out.print(name);

Here is another post I found I feel might work but I don't understand how to implement it into my code or if it would work. Basic way to insert character into string.

The following link is to another similar question. Python vs of Question

This link is what came up when I tried to do a search however I am not using int to string so I don't believe it is relevant. Example of search results

I believe my scenario would benefit others to be able to insert other types of characters into an unknown string. I realize that using / you can insert special characters. However everything I have found so far uses the assumption that you are using a declared variable.

I believe I need to insert the characters I desire spaces and " - " at set points using the .substring(x, x) method however I am unsure how to go about doing so with out messing up my limit of only 14 characters.

Thank you in advance for your help.


Solution

  • If i understand your input output example correctly then the best way is to use regex groups and String::replaceAll like this :

    String[] strings = {"X839WS21R4E877", "x83rws21b3e87a"};
    for(String str : strings){
        str = str.toUpperCase();// to convert the string to UpperCase
        str = str.replaceAll("([A-Z0-9])([A-Z0-9]{3})([A-Z0-9]{4})([A-Z0-9]{2})([A-Z0-9]{3})([A-Z0-9])", 
                "$1-$2 $3 $4-$5 ($6)");
    
        System.out.println(str);
    }
    

    Outputs

    X-839 WS21 R4-E87 (7)
    X-83R WS21 B3-E87 (A)
    

    details

    ([A-Z0-9])([A-Z0-9]{3})([A-Z0-9]{4})([A-Z0-9]{2})([A-Z0-9]{3})([A-Z0-9])
    

    You have to devide your inputs to groups in the previouse regew there are 6 groups :

    • ([A-Z0-9]) groupe 1 matches first letter or numbers
    • ([A-Z0-9]{3}) groupe 3 matches 3 letters or numbers
    • ([A-Z0-9]{4}) groupe 4 matches 4 letters or numbers
    • ([A-Z0-9]{2}) groupe 2 matches 2 letters or numbers
    • ([A-Z0-9]{3}) groupe 3 matches 3 letters or numbers
    • ([A-Z0-9]) groupe 6 matches last character