• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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
  • 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
 
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this help

 
rekha sen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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: 68066
258
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another approach might be the following, of course works slower than regex

 
Campbell Ritchie
Marshal
Posts: 68066
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That approach with a loop might be faster than a regex.
 
What's brown and sticky? ... a stick. Or a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!