Pin It

How to check if a character is a number / digit in java ?

Program to check if a character is a number / digit in Java

package com.hubberspot.example;


public class IsNumberTest {

   public static void main(String[] args) {
        // Create a String of character having numbers 
        // in between just for a test
        String characters = "q1w2E3r4T5y 6!7@8#9$0%-^";

 // we take the length of characters by length()
 int length = characters.length();

 // we than loop each character one by one 
 for(int i = 0; i < length; i++) {
 // we store each character in a variable
 // for each loop
    char character = characters.charAt(i);
         
    // checking for each character that its a number or not
    // by calling Character class isDigit() method , by passing
    // the character. It returns true if the character passed is
    // digit or a number
    if(Character.isDigit(character)) {
         System.out.println("Given character at index " + i + " is a number");
    }
    else {
  System.out.println("Given character at index " + i + " is not a number");
    }
        }

    }

}


Output of the program : 



 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot