Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting String to ?

 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Erik Cederqvist:
Hallo I have a small problem whit my programing.
I read in a String from a objekt but I will have it to a int, double, flot
or somthing else.

thankz for helping me
erik


Sometime, you should be checked your input from object, it's correct type or not.
 
Erik Cederqvist
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo I have a small problem whit my programing.
I read in a String from a objekt but I will have it to a int, double, flot
or somthing else.

thankz for helping me
erik
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

There are static methods in the Integer, Long, Double, and Float classes that can convert a String to an int, long, double, or float, respectively. For example,



Be aware that all these methods can throw NumberFormatExceptions, and a robust program will catch this exception and report that the String didn't contain a valid number.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Double.parseDouble(String s)
it is same for the other numeric types
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that this type of coversion is *not* called "casting"!
 
Erik Cederqvist
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks a lot for the help

have a nice day

erik
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Please note that this type of coversion is *not* called "casting"!


Ilja, thanks for the nice pointer on the subject of the thread... I also have been misled to this thread by reading the subject "casting String to?". I am wondering what kind of object rather than Object does the String can cast to?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic