• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java: Parse a string into an integer (when string has a dollar sign in it)

TJN23

Golden Member
hi everyone, i'd like some assistance if possible:

say i have a string variable called declared that contains the string "$1.25":

static String number = "$1.25";


THE FOLLOWING DOES NOT WORK (obviously, since it can't parse the "$" character right?)

static int parsedNumber = (Integer.parseInt( number ));


anyone have a piece of advice? simple if statement? that sounds too easy

TIA

Tim
 
Well, I'm certainly no Java expert and can't even tell you what commands you need, but with that $ on there, it's seeing it as a string, so you've got to strip if off (something like a Right$ command) then convert the numeric-string to an integer. I don't know of any programming language that will directly convert a mixed alpha-numeric to an integer.
 
Just use a substring method to remove the $.

number=number.subString(1, number.length());

Something along those lines. Syntax and methods may not be correct but that should remove the $ for you. Then you can convert it.
 
i figured it out

just in case anyone was wondering, it goes like this:

public static String removeCharAt(String s, int pos)
{
return s.substring(0,pos)+s.substring(pos+1);
}

it removes a character of incoming String s at position pos

-Tim-
 
Back
Top