• 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

Representing parsed numbers

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you read "numbers" from a text source, how would you represent them?

I'm writing a little parser, and know I can find "number" tokens: for example "4711", "12.75" or "1.5E23". So there can be integer values and float values. Would you always represent the parsed value as a float (or double), and leave it to the user to convert the number to integer when needed? Or would you provide something like "floatValue" and "intValue" and perhaps a method isInteger() so a user can decide which value he wants? Or is there a better way to represent numbers that I haven't thought of?

Just looking for ideas
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know if I am right:

But you can write an IF ELSE block which checks for presence of '.' in the read string and accordingly parses it into an integer or float..


am i correct ??
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you can, but the parsing of the number is not the problem. My question is more like, WOULD you make two cases of it, or simply use a double to save the parsed value regardless if it is an int or float?

I think the best way might be to create a datatype "Number" which holds just a double value, but also offers methods isInteger() and intValue() in case the number found was not a float.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you writing the parser? Are you using regular expressions? Google for JFlex or a similar lexer, find its documentation and look through the handbook. You will probably find a simple example for numbers there (JFlex has one), or there might be something useful in the Java™ Language Specification (try §3.10).

[Pedantic mode]You are not parsing yet, only scanning (or lexing)[/pedantic mode]
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no problem with scanning numbers, only with how you store them...how you represent the scanned value in a token. Do you simply always store it as a double, or do you have two value fields in a number token, one for int, one for double? And a flag indicating isInteger() or the like?

I had a similar case before, and then made a class with two value fields, and a flag. Also with two different methods to access the value of the number. It just seemed awkward, so I was looking for a better solution.

Something I'm just now thinking of, perhaps this would be better:



Although the user of the token still has to check what he gets when calling getValue(), so perhaps the whole topic is just moot..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about returning a java.math.BigDecimal? It has methods such as intValue(), longValue(), doubleValue() etc. to get the value as an int, long, double etc.
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic