cancel
Showing results for 
Search instead for 
Did you mean: 

Take fist 4 char and align

Former Member
0 Kudos

Hi all,

If input is char take fist 4 char ( left alligned),

if input is numeric then , right alligned with leading zeroes.

how to get it..

regards,

ramesh.

Accepted Solutions (0)

Answers (2)

Answers (2)

srikanth_srinivasan3
Active Participant
0 Kudos

Hi

Try this:

for(int i=0;i<args[0].length();i++)

{

if(Character.isDigit(args[0].charAt(i)))

{

args[0];

}

else

{

args[0];

}

}

Create two Fix Values:

One: If the output of above function is "A Number", the pass the actual value's length to FIX VALUES.

Key is the length & Value is the space; Eg: If length is 1, then give 9 spaces in FIX values.

Key Value

1 9 spaces

2 8 spaces

3 7 spaces

Concatenate the output of the FIX Values with actual value at the end or start as per ur requirement.

Similarly for "NOT A NUMBER" too.

-

Srikanth Srinivasan

Edited by: Srikanth Srinivasan on Jun 24, 2010 3:31 PM

Former Member
0 Kudos

Create a UDF with one 'input' parameter


try 
   {  
      Integer.parseInt(input );  
     NumberFormat formatter = new DecimalFormat("000000");
     String s= formatter.format(Double.parseDouble(input));
      return s ;  
   }  
   catch(Exception e)  
  {
      return input.substring(0,4);  
   }  

I have tested the above code..while creating the UDF add the following import to the UDF

java.text.DecimalFormat

java.text.NumberFormat

changed the Decimal format according to your requirement in the above code.

Former Member
0 Kudos

Hi fatima,

My requirement is now changed ..

check the first letter of input ,if char or numeric,

if char

take first 4 chars & left align

if num

add with leading zeros.

& right align

input lenght is 10.

Regards,

Ramesh.

Edited by: Sreeramoju Ramesh on Jun 24, 2010 2:09 PM

rajasekhar_reddy14
Active Contributor
0 Kudos

Have you tested Fatima code,it should work,

Regards,

Raj

Former Member
0 Kudos

Hi,

fatima code is not working completly.

**If input is char take fist 4 char ( left alligned), if input is numeric right alligned with leading zeroes. **

it is giving else part correct but, if it is num it's getting failed.

and it is not taking care of alignments..

input is alphanumeric,we need to apply condition on first char.

If first letter is char then only first 4 digits should be taken

if numeric entire value should be taken ( should add leading zeros to complete 10 digits )

regards,

Ramesh.

Edited by: Sreeramoju Ramesh on Jun 24, 2010 3:30 PM

Former Member
0 Kudos

Hi Ramesh,

Take the first character with substring and find whether it is character or integer. If integer then add zeros else take the first 4 characters.

If your input field is material then map like this:

material ---> substring(0,1) --> udf (CheckNumericORString) ---> if else --> your udf will return 0 if first digit is alphabet else it will send the same input. Then if 0 take first 4 characters else pad with 4 zeros using formatnum(0000). Here you may require to add one more if else condition for checking the total length of input field. If the input length is more than 4 every time then you are ok if it comes less than 4 then that if else condition should take care.

UDF CheckNumericORString:

Create a value udf and name it as CheckNumericORString with one argument and name it as input. Then add this code:

Imports: java.*;

int x = 0;
try{	
Integer.parseInt(input);
return input;                }	
catch(NumberFormatException e){
                      return "0";           }

This will basically return whether the first digit is numeric or character. If numeric same input will return else 0 will return. So after this with if else your issue should be solved.

Regards,

---Satish

Former Member
0 Kudos

Hi satish ,

Input is alphanumeric so we can't use Format number,what else we can do to add it with leading zeros with right align.

Thanks for reply,

Ramesh.

Former Member
0 Kudos

Hi Ramesh,

Use udf given by abhijit in this link which will add leading zeros. Here in the third line instead of 20 put 4 because your field length is 4 or according to your requirement.

Regards,

---Satish

Former Member
0 Kudos

Hi Ramesh,

> Input is alphanumeric so we can't use Format number,what else we can do to add it with leading zeros

Create simple type UDF which is having 2 input fields

1. strVal -- Actual input String

2. totStrLen -- Length of the String (like 10 or...)

int inputValLength =strVal.length();

int totStrLength = Integer.parseInt(totStrLen);

String result="";

if(totStrLength > inputValLength)

for (int i=0; i< totStrLength - inputValLength; i++)

result = result +"0";

return result + strVal;

> Leading zeros with right align.

Default this comes to left alignment, if it is integer then obviously it comes to right alignment.

Regards

Ramesh