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

TJN23

Golden Member
May 4, 2002
1,670
0
0
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
 
 

OZEE

Senior member
Feb 23, 2001
985
0
0
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.
 

Codewiz

Diamond Member
Jan 23, 2002
5,758
0
76
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.
 

TJN23

Golden Member
May 4, 2002
1,670
0
0
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-