File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes casting String to ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "casting String to ?" Watch "casting String to ?" New topic
Author

casting String to ?

somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
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.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Erik Cederqvist
Greenhorn

Joined: Sep 28, 2004
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Double.parseDouble(String s)
it is same for the other numeric types


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Please note that this type of coversion is *not* called "casting"!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Erik Cederqvist
Greenhorn

Joined: Sep 28, 2004
Posts: 3
ok thanks a lot for the help

have a nice day

erik
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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?


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting String to ?