aspose file tools*
The moose likes Beginning Java and the fly likes Handling notation differences with numbers. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Handling notation differences with numbers." Watch "Handling notation differences with numbers." New topic
Author

Handling notation differences with numbers.

Fiorenza Oppici
Ranch Hand

Joined: Jul 22, 2010
Posts: 38
Hi everyone. I developed a small JSP document that can handle the simple task of calculating the square root of a user-posted number, with the purpose of getting started with jsp syntax.
however my question is related to java more in general. : )
the form can of course accept natural numbers and positive decimal numbers but I realized that in my country (as well as in the rest of Europe) we tend to use comma instead of period for representing the decimal separator so more generally people write
123,5
VS
123.5.
this is just the code chunk that does the conversion:


in this way you type with the european standard without problems. my question is: is this an intelligent and elegant approach? could I do better? or it could be better calling an exception everytime you post a comma?

I've got another problem, I was forced to put num.lenght()>1, instead if you post when the field is still blank you get a non-empty string. should I set the default value "" for an input field instead?

thank you in advance. : )


[I study here]
Regards, FiĆ²
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
IMO I wouldn't throw an exception unless you expect your users to know better.
I also believe that it is up to the developer to make software as easy to use
as can be expected. There is a good book called "The Inmates Are Running The Asylum"
that discusses software development and the idea is that sometimes programmers
design their product without taking into consideration ( enough ) the persona of the
end user, and as a result the end product is less user friendly than it could be. A good
read I think.

G

Fiorenza Oppici
Ranch Hand

Joined: Jul 22, 2010
Posts: 38
Geoff Jefferson wrote: IMO I wouldn't throw an exception unless you expect your users to know better.
I also believe that it is up to the developer to make software as easy to use
as can be expected. There is a good book called "The Inmates Are Running The Asylum"
that discusses software development and the idea is that sometimes programmers
design their product without taking into consideration ( enough ) the persona of the
end user, and as a result the end product is less user friendly than it could be. A good
read I think.

G



I do believe in the same approach, code design should meet the user needs and not the programmer's ones, from a big scale to the very very small as in my case. it happened that my boyfriend (He's my official app tester, LOL) entered a comma number and obviously he got back an annoying exception so i decided to handle this scenario, although I don't know if there's a cleverer way to achieve the conversion.
thank you for the reading suggestion.
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 147

Hey!

num.replace(',', '.'); would be shorter. String does have a replace method


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Fiorenza Oppici
Ranch Hand

Joined: Jul 22, 2010
Posts: 38
Hi jan, thank you for the suggestion!
the problem with String.replace() -if I understood the spec-is that it replaces all the occurencies, and surely i don't want that, if there's more than a single comma obviously I call an exception because it's not a number.
but this could be useful in future.
 
 
subject: Handling notation differences with numbers.