I have a scenario where characters like below will be coming in the fixed length file

{ABCDEFGHI = This set of characters represents positive numbers 0123456789 respectively

}JKLMNOPQR = This set of characters represents negative numbers 0123456789 respectively

I need to convert them into corresponding number 0123456789(positive and negative) using java. for example:

45{ should be converted to 450 (as '{' represents positive '0' )

45A should be converted to 451 (as 'A' represents positive '1' )

45B should be converted to 452 (as 'A' represents positive '2' )

45} should be converted to -450 (as '}' represents negative '0' )

45J should be converted to -451 (as 'J' represents negative '1' )

45K should be converted to -452 (as 'K' represents negative '2' )

I am not that good in java so I am using below code to first replace the string containing above mentioned character with the corresponding number using replace function. I know there must be a better way to do that.Can any please suggest me on this.Many thanks in advance.

public static String replaceChar(String str) {
        if (str.contains("{")) {
            str =  str.replace("{", "0");
        }   
        if (str.contains("A")) {
            str =  str.replace("A", "1");
        }
        if (str.contains("B")) {
            str =  str.replace("B", "2");
        }
        if (str.contains("C")) {
            str =  str.replace("C", "3");
        }
        if (str.contains("D")) {
            str =  str.replace("D", "4");
        }
        if (str.contains("E")) {
            str =  str.replace("E", "5");
        }
        if (str.contains("F")) {
            str =  str.replace("F", "6");
        }
        if (str.contains("G")) {
            str =  str.replace("G", "7");
        }
        if (str.contains("H")) {
            str =  str.replace("H", "8");
        }
        if (str.contains("I")) {
            str =  str.replace("I", "9");
        }
        if (str.contains("J")) {
            str =  str.replace("J", "1");
        }
        return str;
    }

2 Answers

0
Eritrean On Best Solutions

You could map your alphabet to digits as @Tim already showed using the letters as key and digits as values, check for negativity using regex and finaly replace map keys contained in input string with map values:

public static void main(String[] args) throws IOException {
    System.out.println(convert("45{"));
    System.out.println(convert("45A"));
    System.out.println(convert("45B"));
    System.out.println(convert("45}"));
    System.out.println(convert("45J"));
    System.out.println(convert("45K"));
}
public static long convert(String str){
    String alphabet = "{ABCDEFGHI}JKLMNOPQR";
    Map<String,String> map = Pattern.compile("")
                              .splitAsStream(alphabet)
                              .collect(Collectors.toMap(k->k, k->String.valueOf(alphabet.indexOf(k)%10)));
    boolean isNegative = false;
    if(Pattern.compile("[J-R\\}]+").matcher(str).find()){
        isNegative = true;
    }
    for(String letter : alphabet.split("")){
        str = str.replace(letter, map.get(letter));
    }
    long result = Long.parseLong(str);
    return isNegative ? - result: result;
}
0
Tim Biegeleisen On

We can try iterating over the string, and then use the modulus to compute the numerical value:

String input = "ABCDEFGHIJKLMNOPQR";
StringBuilder sb = new StringBuilder();

for (int i=0; i < input.length(); ++i) {
    sb.append(String.valueOf((input.charAt(i) - 'A') % 10));
}

System.out.println(input);
System.out.println(sb.toString());

ABCDEFGHIJKLMNOPQR
012345678901234567

My output does line up exactly with what you have in your question, but your question appears to have some typos in the letter strings.