For Programmers: Free Programming Magazines  


Home > Archive > Java Help > January 2008 > DecimalFormat and positive integers









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author DecimalFormat and positive integers
Giovanni D'Ascola

2008-01-09, 7:25 pm

Hi
I want to parse a string in a positive integer number.
I've already tried with the following code:

//...
DecimalFormat decForm = new DecimalFormat();
decForm.setNegativePrefix(null); // <-- note this!
decForm.applyPattern("#");
ParsePosition pos = new ParsePosition(0);
decForm.parse(a_string, pos);
//...

but this code doesn't work: it parses strings like "-5".
Can you help me, please?
Thanks in advance.
Giovanni D'Ascola

2008-01-09, 7:25 pm

Giovanni D'Ascola ha scritto:
> Hi
> I want to parse a string in a positive integer number.
> I've already tried with the following code:
>
> //...
> DecimalFormat decForm = new DecimalFormat();
> decForm.setNegativePrefix(null); // <-- note this!
> decForm.applyPattern("#");
> ParsePosition pos = new ParsePosition(0);
> decForm.parse(a_string, pos);
> //...
>
> but this code doesn't work: it parses strings like "-5".
> Can you help me, please?
> Thanks in advance.


I've just found a workaround, something like this:

if ( decForm.getNegativePrefix().charAt(0) == value.charAt(0) )
// don't parse the string...
Patricia Shanahan

2008-01-09, 7:25 pm

Giovanni D'Ascola wrote:
> Giovanni D'Ascola ha scritto:
>
> I've just found a workaround, something like this:
>
> if ( decForm.getNegativePrefix().charAt(0) == value.charAt(0) )
> // don't parse the string...


There is also a more general solution - first parse the integer as it
is, then apply any range checks. Treat the requirement that the integer
be non-negative exactly the same way as you would treat a rule that it
has to be strictly positive, or no greater than 10.

Patricia
John W. Kennedy

2008-01-09, 7:25 pm

Giovanni D'Ascola wrote:
> Giovanni D'Ascola ha scritto:
>
> I've just found a workaround, something like this:
>
> if ( decForm.getNegativePrefix().charAt(0) == value.charAt(0) )
> // don't parse the string...


The applyPattern is clobbering the setNegativePrefix.
Do the applyPattern before the setNegativePrefix.
Better, do new DecimalFormat("#") and leave off applyPattern.

But if you do that, you'll get a NullException.
Set the negative prefix to "", not null.
--
John W. Kennedy
If Bill Gates believes in "intelligent design", why can't he apply it to
Windows?
Giovanni D'Ascola

2008-01-10, 4:34 am

Giovanni D'Ascola ha scritto:
> Giovanni D'Ascola ha scritto:
>
> I've just found a workaround, something like this:
>
> if ( decForm.getNegativePrefix().charAt(0) == value.charAt(0) )
> // don't parse the string...


The previous workaround i posted actually doesn't work, it parses
strings like "5,5".
In the end, i think the Integer class is more friendly:

//...
try {
int num = Integer.parseInt(value);
if (num < 0)
return false;
else
return true;
}
catch (Exception e) {
return false;
}
//...
Lew

2008-01-10, 8:16 am

Giovanni D'Ascola wrote:
> In the end, i think the Integer class is more friendly:
>
> //...
> try {
> int num = Integer.parseInt(value);
> if (num < 0)
> return false;
> else
> return true;
> }
> catch (Exception e) {
> return false;
> }
> //...


Yes, Patricia Shanahan consistently gives good advice.

--
Lew
Giovanni D'Ascola

2008-01-10, 7:22 pm

Lew ha scritto:
> Giovanni D'Ascola wrote:
>
> Yes, Patricia Shanahan consistently gives good advice.
>

Yes, brava Patricia.
Lew

2008-01-10, 7:22 pm

Lew ha scritto:

Giovanni D'Ascola wrote:[color=darkred]
> Yes, brava Patricia.


And bravo! Giovanni for finding a solution with the help of his friends.

--
Lew
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com