Zoo tutorials: [ SQL | Linux | XML ]
ProgZoo: [ Java | C# | VB | C++ | Perl ]
Log in

A Gentle Introduction to
Programming

Converting a String to a number

 

Data types

Converting a String to a number

1. [ Java ] String to int


Big

The static method Integer.parseInt will do the trick

There is a danger that the string does not represent a valid number in which case an exception will be raised.

There are plenty of similar routines such as: Float.parseFloat Long.parseLong Double.parseDouble Boolean.parseBoolean

2. [ Java ] Catch the exception


Big

If the string is not a valid integer then an exception is raised.