• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing an Integer, Double, Date, etc  RSS feed

 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone! I'm new to Java and have a question about parsing numeric (primitive?) types.

In VB.NET, when the program I was working in required some user input, and this had to be a valid INT for example, I used the following to attempt to parse the user input to an INT value:



Integer/Double/Date/etc TryParse methods returned a boolean value indicating where the data passed to it could be parsed or not, and if the conversion succeeded it stored the valid converted value in the second argument passed to it so you could use it afterward.

So for the actual question. Is there a similar method in Java? Or should I write my own validation code?
In that case, what would you say is the proper way to do it? Maybe something like int i = Integer.parseInt(someString) with a Try/Catch block?

Thanks in advance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, you have to try/catch.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Charles,

yup, try/catch with Integer.parseInt(...)

Greetings,
Piet
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys! I have learned a lot reading these forums in the last few days. Your help is much appreciated.



 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!