> > > How-to Pad Leading Zeros to a String Value- V2 Java Ruleset

How-to Pad Leading Zeros to a String Value- V2 Java Ruleset

20th November 2019

Cleo Clarify JAVA program to pad string values

As part of our Cleo Clarify JAVA V2 series, we are teaching users different ways to use JAVA to create 

In our Quick Reference Guide we will show another example of how to create an easy to use JAVA class to add leading zeros, or any other leading value.

JAVA Scenario:

The Cleo Clarify developer wants to pad leading zeros to a numeric value.

The JAVA program (below) will accept 4 input values and will return one output value

JAVA Set-up

Parameters:

INPUTS
input - String, data to be padded
length - String, length of output
side - String, which side to pad value left/right
character - String, char to pad value with

OUTPUTS
Output Value

Tests:

Example 1: Left padding
Inputs: input: abc, length: 10, side: left, character:0
Result: “0000000abc”

Example 2: Right padding
Inputs: input: abc, length: 10, side: right, character:0
Result: “abc0000000”

NOTE: In case ’ ’ padding is required on a numeric input, always get the output from this action and RawMove to the final destination

JAVA Program:

package com.abc.core.actions;

import com.extol.ebi.lang.annotations.EbiName;
import com.extol.ebi.reactor.lib.AbstractAction;

@EbiName(“Padding value with char”)
public class PadValueWithChar extends AbstractAction{

String stringChar = “ ”;
String inputStr = “”;
String outputStr = “”;
String side = “”;
String zero = “0”;
char inputChar = ‘0’;
int strLength = 0;
int inputLength = 0;
int actualLength = 0;
int positionLength = 0;

public com.extol.ebi.ruleset.lang.core.String execute (
@EbiName(“input”) com.extol.ebi.ruleset.lang.core.String input,
@EbiName(“length”) com.extol.ebi.ruleset.lang.core.Number length,
@EbiName(“side”) com.extol.ebi.ruleset.lang.core.String side,
@EbiName(“character”) com.extol.ebi.ruleset.lang.core.String stringChar

) {

//check if length  is null
if (length != null) {
strLength = length.asJavaInteger().intValue();
}

//check if input is null
if (input != null) {
inputStr = input.asJavaString();
}

//check if side is null
if (side !=null) {
if (side.asJavaString().toLowerCase() ==“right”) {
strLength = -strLength;
//Right padded output
outputStr = String.format(“%1$” + strLength + “s”, inputStr);
} else {
//Left padded output
outputStr = String.format(“%1$” + strLength + “s”, inputStr);
}
}

//Check if padding is needed with unique a character
if (stringChar != null) {
{
inputChar = stringChar.asJavaString().charAt(0);
outputStr = String.format(“%1$” + strLength + “s”, inputStr).replace(’ ’, inputChar);
}
}

return this.asString(outputStr);
}

}


By: on