Velocity Reviews > Java > True Integer, Double

# True Integer, Double

RC
Guest
Posts: n/a

 05-12-2008
Heaveno (Not Hello),

How do I make sure a number is a TRUE Integer or TRUE Double?

i.e
Integer integer = new Integer("1"); // TRUE

Integer integer = new Integer("1.0");
// NOT TRUE, NumberFormatException couldn't catch it

Double d = new Double("1.0"); // TRUE

Double d = new Double("1");
// NOT TRUE, NumberFormatException couldn't catch it

Thank Q very much in advance!

Stefan Ram
Guest
Posts: n/a

 05-12-2008
RC <(E-Mail Removed)> writes:
>How do I make sure a number is a TRUE Integer or TRUE Double?
>Thank Q very much in advance!

I might be able to answer as soon as »TRUE Integer« is defined.

RC
Guest
Posts: n/a

 05-12-2008
Eric Sosman wrote:

>
> Try explaining your notion of "TRUE" a bit more, and/or
> consider using java.text.NumberFormat.

I have some text data look like this (written by some
retiree in Fortran)
I need to convert them into XML file

1295 1 38.23 39.26 41.10 43.82 47.50 45.29

Which means data starts from
<event data="1995-12-1 00:00:00" value="38.23" />
<event data="1995-12-1 06:00:00" value=" 39.26" />
<event data="1995-12-1 12:00:00" value=" 41.10" />
<event data="1995-12-1 18:00:00" value=" 43.82" />
<event data="1995-12-2 00:00:00" value=" 47.50" />
<event data="1995-12-2 06:00:00" value=" 45.29" />

Now, you can see column 0 and column 1(1st and 2nd columns)
are TRUE Integer.
After column 2 (3rd column) are TRUE Double.

Stefan Ram
Guest
Posts: n/a

 05-12-2008
RC <(E-Mail Removed)> writes:
>Now, you can see column 0 and column 1(1st and 2nd columns)
>are TRUE Integer.

A common kind of question/dialogs in this newsgroup
goes like:

A: A text like »egnor« is an alpha, while a
text like »aeowr« is a beta. Now, I need
a program or a regular expression to tell
whether any given text is an alpha or a beta.

B: Could you define what is an alpha?

A: Well, yes, it is a text like »egnor«,
but not a text like »aeowr«, you see?

Wild guess:

The simplest solution for you might be String#contains( "." ).

John B. Matthews
Guest
Posts: n/a

 05-12-2008
In article <(E-Mail Removed)>,
RC <(E-Mail Removed)> wrote:
[...]
> I have some text data look like this
> (written by some retiree in Fortran)

Indeed.

[...]
> 1295 1 38.23 39.26 41.10 43.82 47.50 45.29

[...]

A suitable abstraction might be java.lang.Number. The parse() method of
java.text.DecimalFormat should suffice.

Wojtek
Guest
Posts: n/a

 05-12-2008
RC wrote :
> Eric Sosman wrote:
>
>>
>> Try explaining your notion of "TRUE" a bit more, and/or
>> consider using java.text.NumberFormat.

>
> I have some text data look like this (written by some
> retiree in Fortran)
> I need to convert them into XML file
>
> 1295 1 38.23 39.26 41.10 43.82 47.50 45.29
>
> Which means data starts from
> <event data="1995-12-1 00:00:00" value="38.23" />
> <event data="1995-12-1 06:00:00" value=" 39.26" />
> <event data="1995-12-1 12:00:00" value=" 41.10" />
> <event data="1995-12-1 18:00:00" value=" 43.82" />
> <event data="1995-12-2 00:00:00" value=" 47.50" />
> <event data="1995-12-2 06:00:00" value=" 45.29" />
>
> Now, you can see column 0 and column 1(1st and 2nd columns)
> are TRUE Integer.
> After column 2 (3rd column) are TRUE Double.

Try to create an Integer, and if that fails then assume it is a Double.

--
Wojtek

Arved Sandstrom
Guest
Posts: n/a

 05-12-2008
"RC" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Eric Sosman wrote:
>
>>
>> Try explaining your notion of "TRUE" a bit more, and/or
>> consider using java.text.NumberFormat.

>
> I have some text data look like this (written by some
> retiree in Fortran)
> I need to convert them into XML file
>
> 1295 1 38.23 39.26 41.10 43.82 47.50 45.29
>
> Which means data starts from
> <event data="1995-12-1 00:00:00" value="38.23" />
> <event data="1995-12-1 06:00:00" value=" 39.26" />
> <event data="1995-12-1 12:00:00" value=" 41.10" />
> <event data="1995-12-1 18:00:00" value=" 43.82" />
> <event data="1995-12-2 00:00:00" value=" 47.50" />
> <event data="1995-12-2 06:00:00" value=" 45.29" />
>
> Now, you can see column 0 and column 1(1st and 2nd columns)
> are TRUE Integer.
> After column 2 (3rd column) are TRUE Double.

The first two numbers in the text file written by the retiree are probably
header lines, something pretty commonly used in flat files by us geriatric
types. The usual way of handling a situation like this is to understand the
format of the file, read the header if necessary (and use it if necessary -
these two values are probably # of data values and maybe # of fields), and
then read all the data, *knowing* that the data are floating point.

AHS

Mark Space
Guest
Posts: n/a

 05-12-2008
Wojtek wrote:
> RC wrote :

>> I need to convert them into XML file
>>
>> 1295 1 38.23 39.26 41.10 43.82 47.50 45.29
>>

> Try to create an Integer, and if that fails then assume it is a Double.
>

No that won't work. The OP already showed us that trying to create an
Integer from float-string succeeds.

RC, I think what you want to do is *validate* your data before you try
to parse it.

If the data must contain only decimal digits, use a regex to match that
before you call Integer.parse(). If the data must contain at least one
"." and one decimal digit, then use a regex to verify that fact, again
before calling Double.parse().

Parse() doesn't validate data, I guess, just parses it. Validation you
do yourself.

Roedy Green
Guest
Posts: n/a

 05-12-2008
On Mon, 12 May 2008 10:50:14 -0400, RC <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>How do I make sure a number is a TRUE Integer or TRUE Double?

what constitutes a integer or double in Java source is different that
in data. Data is laxer since you KNOW the type to be coerced to.

However if you wanted to enforce stricter rules, write a regex to
describe your allowed patterns (e.g. precise number of decimal
places), or see what effects you can get with a DecimalFormat and a

See http://mindprod.com/jgloss/regex.html
http://mindprod.com/jgloss/decimalformat.html
--

The Java Glossary
http://mindprod.com

Arne Vajhøj
Guest
Posts: n/a

 05-12-2008
Mark Space wrote:
> Wojtek wrote:
>> Try to create an Integer, and if that fails then assume it is a Double.

>
> No that won't work. The OP already showed us that trying to create an
> Integer from float-string succeeds.
>
> RC, I think what you want to do is *validate* your data before you try
> to parse it.
>
> If the data must contain only decimal digits, use a regex to match that
> before you call Integer.parse(). If the data must contain at least one
> "." and one decimal digit, then use a regex to verify that fact, again
> before calling Double.parse().
>
> Parse() doesn't validate data, I guess, just parses it. Validation you
> do yourself.

Did he show that ?

Have you tried ?

Arne