True Integer, Double

R

RC

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!
 
S

Stefan Ram

RC said:
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.
 
R

RC

Eric said:
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.
 
S

Stefan Ram

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

John B. Matthews

RC said:
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.
 
W

Wojtek

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.

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

Arved Sandstrom

RC said:
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
 
M

Mark Space

Wojtek said:
RC 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.
 
R

Roedy Green

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
mask using the parse method.

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

Arne Vajhøj

Mark said:
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
 
A

Arne Vajhøj

RC said:
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
 
P

Patricia Shanahan

Arne said:
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
 
M

Mark Space

Eric said:
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
about that.

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

Arne Vajhøj

Patricia said:
...

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
 
A

Arne Vajhøj

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

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
 
R

RC

Eric said:
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.
 
A

Arne Vajhøj

RC said:
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
 
T

Tom Anderson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top