cancel
Showing results for 
Search instead for 
Did you mean: 

Help needed on java code

Former Member
0 Kudos

Hi all,

In the below code, if condition is failing. what might be the reason.......??

String mAmtBaseStr = "-00000000000200.00";
		
		if (mAmtBaseStr.substring(0).equals("-"))
		{
			//strip "-" from first char
			int len1 = mAmtBaseStr.length();
			mAmtBaseStr = mAmtBaseStr.substring((1 - 0),len1);
			
			//put the stripped "-" at last
			mAmtBaseStr = mAmtBaseStr + "-";
			
							
		}

I tried other options like,

if (mAmtBaseStr.substring(0,1).equals("-")) etc etc......

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi

mAmtBaseStr.substring(0)

basically does nothing as it takes the entire string.

mAmtBaseStr.substring(0,1)

should give you the first string

you can also use this code


String mAmtBaseStr = "-00000000000200.00";
		
		if (mAmtBaseStr.contains("-"))
		{
			//strip "-" from the string wherever it may be
			mAmtBaseStr = mAmtBaseStr.replace("-","");
			
			//put the stripped "-" at last
			mAmtBaseStr = mAmtBaseStr + "-";
			
							
		}

Answers (2)

Answers (2)

anupam_ghosh2
Active Contributor
0 Kudos

Hi,

This code fails because of this reason



_problem1 _

String mAmtBaseStr = "-00000000000200.00";
mAmtBaseStr.substring(0)-------> -00000000000200.00 [as substring(0) returns entire string starting from index zero.]
when you are writing 
if (mAmtBaseStr.substring(0).equals("-"))
{

     You are tryring to check if the value mAmtBaseStr.substring(0) [ "-00000000000200.00"] is equal to string "-" or not.
      As you can see the strings are not equal the if condition never returns you true value.

}

_Problem2_

you are nowhere checking whether the inbound string is null or empty. In case the string comes as null or empty java will throw an exception and your mapping will fail. So if input string does not begin with an hyphen you need to pass it as it is

Thus I would modify your code a little bit to make it work


public static String removeHyphen(String mAmtBaseStr)
	{
		
		if (mAmtBaseStr!=null && mAmtBaseStr!="" && mAmtBaseStr.substring(0,1).equals("-"))
		{
			//strip "-" from first char
			int len1 = mAmtBaseStr.length();
			mAmtBaseStr = mAmtBaseStr.substring((1 - 0),len1);
			
			//put the stripped "-" at last
			mAmtBaseStr = mAmtBaseStr + "-";
			
							
		}
		return mAmtBaseStr;



	}

This code works and produces required output

input: -00000000000200.00

output: 00000000000200.00-

Regards

Anupam

Former Member
0 Kudos

Thank you guys,

if (mAmtBaseStr.contains("-")) did worked.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

You might want to try this... This should work

String mAmtBaseStr = "-00000000000200.00";
		
		if (mAmtBaseStr.contains("-"))
		{
			//strip "-" from first char
			mAmtBaseStr = mAmtBaseStr.substring(1 , mAmtBaseStr.length());
			//put the stripped "-" at last
			mAmtBaseStr = mAmtBaseStr + "-";
			
							
		}