- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to check if a string is a valid number
To check if a string is a valid Integer, use the Integer.parseInt() method, with the string to be checked passed as a parameter.
To check if a string is a valid Float, use the Float.parseFloat() method, with the string to be checked passed as a parameter.
The following is an example that checks for valid integer.
Example
public class Demo { public static void main(String args[]) throws Exception { String id = "100"; int val = Integer.parseInt(id); System.out.println(val); } }
Output
100
The following is an example that checks for valid float.
Example
public class Demo { public static void main(String args[]) throws Exception { String id = "100.5"; float val = Float.parseFloat(id); System.out.println(val); } }
Output
100.5
Advertisements