# True Integer, Double

Discussion in 'Java' started by RC, May 12, 2008.

1. ### RCGuest

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!

RC, May 12, 2008

2. ### Stefan RamGuest

RC <> 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.

Stefan Ram, May 12, 2008

3. ### RCGuest

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.

RC, May 12, 2008
4. ### Stefan RamGuest

RC <> 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( "." ).

Stefan Ram, May 12, 2008
5. ### John B. MatthewsGuest

In article <>,
RC <> 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.

John B. Matthews, May 12, 2008
6. ### WojtekGuest

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

Wojtek, May 12, 2008
7. ### Arved SandstromGuest

"RC" <> wrote in message
news:...
> 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

Arved Sandstrom, May 12, 2008
8. ### Mark SpaceGuest

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.

Mark Space, May 12, 2008
9. ### Roedy GreenGuest

On Mon, 12 May 2008 10:50:14 -0400, RC <>
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

Roedy Green, May 12, 2008
10. ### Arne VajhøjGuest

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

Arne Vajhøj, May 12, 2008
11. ### Arne VajhøjGuest

RC wrote:
> 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.

That looks completely trivial to me.

Any retiree should be able to code that in 5 minutes.

Maybe you should find one and ask for help.

You can do it in so many different ways.

Integer parseInt and Double parseDouble

Scanner hasNextInt/hasNextDouble/nextInt/nextDouble

do everything using String's

etc.

Arne

Arne Vajhøj, May 12, 2008
12. ### Patricia ShanahanGuest

Arne Vajhøj wrote:
> RC wrote:
>> 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.

>
> That looks completely trivial to me.

....

In so far as there is any difficulty, it lies in lack of specification.
What is the syntax for a "TRUE Integer"? What is the syntax for a "TRUE
Double"? Is there any overlap between them, and if so what should be
done with strings in the overlap? Is internationalization required?

Patricia

Patricia Shanahan, May 13, 2008
13. ### Mark SpaceGuest

Eric Sosman wrote:

>
> If that's what he was trying to say, he was mistaken.
> On Java 1.6.0_05-b13, `new Integer("1.0")' gives me
>
> Exception in thread "main" java.lang.NumberFormatException: For input
> string: "1.0"

Huh, so he did. I'm honestly not sure where I got that idea. Sorry

Still, I like the idea of a general data validator. Seems cleaner, and
possibly faster, than just throwing exceptions.

Mark Space, May 13, 2008
14. ### Arne VajhøjGuest

Patricia Shanahan wrote:
> Arne Vajhøj wrote:
>> RC wrote:
>>> 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.

>>
>> That looks completely trivial to me.

> ...
>
> In so far as there is any difficulty, it lies in lack of specification.
> What is the syntax for a "TRUE Integer"? What is the syntax for a "TRUE
> Double"? Is there any overlap between them, and if so what should be
> done with strings in the overlap? Is internationalization required?

But using standard free format integer and decimal with period as
decimal separator will probably be OK and will certainly be a good
starting point for enhancements.

Arne

Arne Vajhøj, May 13, 2008
15. ### Arne VajhøjGuest

Mark Space wrote:
> Eric Sosman wrote:
>> If that's what he was trying to say, he was mistaken.
>> On Java 1.6.0_05-b13, `new Integer("1.0")' gives me
>>
>> Exception in thread "main" java.lang.NumberFormatException: For input
>> string: "1.0"

>
> Huh, so he did. I'm honestly not sure where I got that idea. Sorry
>
> Still, I like the idea of a general data validator. Seems cleaner, and
> possibly faster, than just throwing exceptions.

That should only be relevant if errors happen frequently and the program
logic requires the program to continue running.

But validating via regex should be relative easy, so no problem
doing that either.

Arne

Arne Vajhøj, May 13, 2008
16. ### RCGuest

Eric Sosman wrote:

>
> If that's what he was trying to say, he was mistaken.
> On Java 1.6.0_05-b13, `new Integer("1.0")' gives me
>
> Exception in thread "main" java.lang.NumberFormatException: For input
> string: "1.0"
>
> ... so I don't think his "NumberFormatException couldn't catch
> it" meant "No exception is thrown." (What it *did* mean is a
> mystery he's declined to elucidate.)

We are not allows to use 1.6.x, yet.
Currently all our projects MUST use 1.5.x

Thank Q for all of u.
P.S.
The reason I DON'T want to use

new Integer("1");
and
new Double("1.0");
is:

the text format sometime is

1295 1 38.23 39.26 41.10 43.82 47.50 45.29

The data starts at column 2 (3rd column)
But some time is

JOSNC 0393 29 14.17

The data starts at column 3(4th column), also the 1st column is a string.

There are more formats to come (U know those Fortran programmers during
70's). We have to clean up their mess, all into XML format.

RC, May 13, 2008
17. ### Arne VajhøjGuest

RC wrote:
> Eric Sosman wrote:
>> If that's what he was trying to say, he was mistaken.
>> On Java 1.6.0_05-b13, `new Integer("1.0")' gives me
>>
>> Exception in thread "main" java.lang.NumberFormatException: For input
>> string: "1.0"
>>
>> ... so I don't think his "NumberFormatException couldn't catch
>> it" meant "No exception is thrown." (What it *did* mean is a
>> mystery he's declined to elucidate.)

>
> We are not allows to use 1.6.x, yet.
> Currently all our projects MUST use 1.5.x

> The reason I DON'T want to use
>
> new Integer("1");
> and
> new Double("1.0");
> is:
>
> the text format sometime is
>
> 1295 1 38.23 39.26 41.10 43.82 47.50 45.29
>
> The data starts at column 2 (3rd column)
> But some time is
>
> JOSNC 0393 29 14.17
>
> The data starts at column 3(4th column), also the 1st column is a string.

That is not a reason not to use those - you just need to check data
before calling them.

There are other reasons why I would not use those, but ...

> There are more formats to come (U know those Fortran programmers during
> 70's). We have to clean up their mess, all into XML format.

So far I have not seen any indication of that they have not done
a good job - it is not their faults that some of todays programmers
seems to have problems with extremely simple tasks.

Arne

Arne Vajhøj, May 15, 2008
18. ### Tom AndersonGuest

On Sat, 17 May 2008, Tim Smith wrote:

> In article <>,
> RC <> wrote:
>
>> The reason I DON'T want to use
>>
>> new Integer("1");
>> and
>> new Double("1.0");
>> is:
>>
>> the text format sometime is
>>
>> 1295 1 38.23 39.26 41.10 43.82 47.50 45.29
>>
>> The data starts at column 2 (3rd column)
>> But some time is
>>
>> JOSNC 0393 29 14.17
>>
>> The data starts at column 3(4th column), also the 1st column is a string.
>>
>> There are more formats to come (U know those Fortran programmers during
>> 70's). We have to clean up their mess, all into XML format.

>
> So, you are dealing with a bunch of messy text formats, with special
> cases and such, that need to be cleaned up?
>
> Are you *required* to use Java for this? Your problem seems to be
> getting into the realm where Perl might be more convenient. This is the
> kind of thing it was designed for.

Perl was designed? I thought Larry Wall just found it growing under a log
one day.

tom

--
1 p4WN 3v3Ry+h1n G!!!

Tom Anderson, May 18, 2008