on 02-01-2012 9:26 PM
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......
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 + "-";
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 + "-";
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.