• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling notation differences with numbers.

 
Fiorenza Oppici
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. : )
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!

num.replace(',', '.'); would be shorter. String does have a replace method
 
Fiorenza Oppici
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic