• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to test if a given exponential number is a numeric number not containing any alphabet.

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have some numeric values like this, which needs to be tested to see if there are numeric or alpha numeric.

I need to get only numeric values eliminating the alpha numeric values in the process.

Some of the numeric values are like this,

3.04005885553944E-06
1.19449873607102E-05
8.2121788104883E-06
5.22593197031073E-06
4.47937026026634E-06
2.98624684017756E-06
1.
3.73280855022195E-06

I WROTE THIS CODE AND IT IS NOT WORKING FOR THE ABOVE NUMBERS





WHat I need to change to accept the above type of numeric values.

Thanks in advance
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this help

 
rekha sen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:Does this help




Sir,

I am getting numeric values from a file. I need to check each of the line contains numeric value only.
Some numeric values contain exponential values and some contain values like 46.
I need to consider all the numbers which do not contain an alpha value in it and load it into db.

The code I wrote, is not considering exponential and number like this 46. or 1.

How do I consider these kind of values has numeric values.
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may write a function that tries to convert the String into a valid number, if it fails means it might be having alphabets.

 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rekha sen wrote:
Some of the numeric values are like this,

3.04005885553944E-06
1.19449873607102E-05
8.2121788104883E-06
5.22593197031073E-06
4.47937026026634E-06
2.98624684017756E-06
1.
3.73280855022195E-06

I WROTE THIS CODE AND IT IS NOT WORKING FOR THE ABOVE NUMBERS





WHat I need to change to accept the above type of numeric values.



None of those will match your regular expression. Your use of matches() means that the whole line must match and those items with an E cannot match because of the E. The value "1." cannot match because your regular expression insists on having at least one decimal after the decimal point.

To match all your example values you must add to the reges a term to deal with the E and you must allow zero decimals after the decimal point. Simple enough.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:You may write a function that tries to convert the String into a valid number, if it fails means it might be having alphabets.



Using your code what would happen if the number was 1.2345E1000 ?
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course in this case it will be true.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:Of course in this case it will be true.



It wasn't obvious to me but you are right it does pass though the resulting value is infinity but of course this does not matter to the OP.
 
rekha sen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:

Swastik Dey wrote:You may write a function that tries to convert the String into a valid number, if it fails means it might be having alphabets.



Using your code what would happen if the number was 1.2345E1000 ?




Sir,

I have made a change to the method to accept the Exponential values and it is like this,



This method is handling every thing except "1." values.

Please help me in changing the regex to accept numbers like "1." or "100."

Thank You.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rekha sen wrote:
I have made a change to the method to accept the Exponential values and it is like this,



This method is handling every thing except "1." values.

Please help me in changing the regex to accept numbers like "1." or "100."
.



Using "[0-9]+" to define what can come after the decimal point says that there must always be at least one decimal character. Change the '+' to a '*' and it says any number of decimal characters which includes none at all!

If you are going to use regex in the future you would do well to put in effort to learn about them.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is “1.” a valid format for a number?
You could try wrapping everything from the \\. to before the eE in a (…)?
Or change the + after \\.[0-9] to *
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another approach might be the following, of course works slower than regex

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That approach with a loop might be faster than a regex.
reply
    Bookmark Topic Watch Topic
  • New Topic