• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Getting Error on Non Number

 
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try it figure out why I get this error message:

java.lang.NumberFormatException: For input string: ""

On this code:
 
Marshal
Posts: 6036
429
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is GenericPrams.QMS_ALLOWED_CHARS_NUMBERS? A regex expression I assume? What is its value?
 
Tim Cooke
Marshal
Posts: 6036
429
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to ask my next question ahead of time.... What are you expecting getParameter("overridepercent") to yield?
 
Tim Cooke
Marshal
Posts: 6036
429
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And finally, what Integer value are you expecting to get out of the whole thing?
 
Rancher
Posts: 5128
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the value returned by this expression:  request.getParameter("overridepercent")
Add a print statement before the posted statements to show its value.  Be sure to add delimiters around the value in the print statement so blanks will show:  
 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:What is GenericPrams.QMS_ALLOWED_CHARS_NUMBERS? A regex expression I assume? What is its value?



 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:And to ask my next question ahead of time.... What are you expecting getParameter("overridepercent") to yield?



Any number or combination of numbers 0-9
 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:And finally, what Integer value are you expecting to get out of the whole thing?



A number representing a percent.
 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I have posted this in error.
I just realized I made some adjustments to my code base after the error was generated so the line of code the error says it happened in is not what the current line of code is at now.
Sorry.
 
Saloon Keeper
Posts: 28797
212
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the interest of readability:

I'm afraid that the original not only is likely not to fit on smaller displays, but also our formatter tend to obscure critical text.

Note that if you use an IDE's automated pretty-printer, some (depending on settings) may put the "." operators at the end of the line, not the beginning, but I find the operator-at-the-beginning more informative to me.

As for general nit-picking, I'm certainly one for complex statements, but this is a bit much. Also, there are two reasons why breaking this up into simpler statements is a good idea (indentation or not).

First, if you have to step through the code with a debugger, debuggers step per-statement so a LOT of details will be more or less invisible in a complex statement.

Secondly, if you break the code into smaller sequences, it's easier to capture the intermediate values in case you want to print them out to a debug log.

And, if you are REALLY paranoid, you can always put assertions into the code, although I've never seen anyone use the Java assert the way it is in C/C++. Partly, I think because Java's assert is overly complex. Instead we prefer unit-test assertion methods.

Last, but not least, as I've mentioned elsewhere, I prefer to document the allowable data values that go into code. We could, based on context, assume that the percentage can be blank or a number from 0 to 100, but maybe you work for a firm that demands 100%. Or gets negative percentages.

The statement also seems to try to sanitize data. I'm not so kind. If they cram in non-numeric characters into a numeric field, I'm going to reject the whole request. Who knows what else they messed up?
 
Sheriff
Posts: 9021
656
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:The statement also seems to try to sanitize data. I'm not so kind. If they cram in non-numeric characters into a numeric field, I'm going to reject the whole request. Who knows what else they messed up?


Totally.

@OP
i.e. abc1def4. If possible in your use case, you may can to return 400 Bad Request.

Try this version of formatting (note, I extracted some code to variables and inverted some logic, for better readability(?)):
 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Try this version of formatting (note, I extracted some code to variables and inverted some logic, for better readability(?)):



Okay I changed my code to:



I am still getting this:

java.lang.NumberFormatException: For input string: ""

This is my client code:

 
Marshal
Posts: 4842
609
VSCode Eclipse IDE Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the value of overrideValue after this is executed?
 
Ron McLeod
Marshal
Posts: 4842
609
VSCode Eclipse IDE Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try printing/logging the value:
 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:What is the value of overrideValue after this is executed?



The default value for the input is a zero.
It is a space. The user is wiping out the zero using the space bar.
Seems like my client code reads a space as allowed.
How do I check for a space and not allow it?
Would I use a regex in lieu of the jQuery trim method?

 
Ron McLeod
Marshal
Posts: 4842
609
VSCode Eclipse IDE Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:How do I check for a space and not allow it?
Would I use a regex in lieu of the jQuery trim method?



Try using isBlank() rather than isEmpty():
 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This does not seem to work either:

 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Try using isBlank() rather than isEmpty():



isBlank() does not seem to be a method of Java String class
 
Ron McLeod
Marshal
Posts: 4842
609
VSCode Eclipse IDE Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the client side, couldn't you just use the JS trim() method?
 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I am using Java 8
 
Ron McLeod
Marshal
Posts: 4842
609
VSCode Eclipse IDE Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:Sorry, I am using Java 8


overrideValue.trim().isEmpty()
 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:On the client side, couldn't you just use the JS trim() method?



This does not prevent spaces either:

 
Steve Dyke
Ranch Hand
Posts: 2286
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:
overrideValue.trim().isEmpty()



I will give this a try. Thanks.
 
Ron McLeod
Marshal
Posts: 4842
609
VSCode Eclipse IDE Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:This does not prevent spaces either


trim() does work, but I don't know about the jQuery bits through or what else is in your if-else blockedit: added check for == ""
 
Tim Holloway
Saloon Keeper
Posts: 28797
212
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have never done any of these. Too complicated/too much overhead.

I do a a simple Integer.valueOf(string) type conversion. If it throws a NumberFormatConversion exception, I reject it. Otherwise I consider it good enough.

If I was really obsessive, I could do a character-by-character isDigit() test, but again, it's not worth the overhead to me.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic