Check the Character/First Character Of a String is Number or Not in Java

Core Java » on Oct 14, 2012 { 5 Comments } By Sivateja

Let us see how to find whether the given Character of a String is a number or first Character of any String is a number or not ?

package java4s;

public class CharNumber {

      public static void main(String... args)
          String str = "java4s";

          //Check whether the given character is a number of not ?

          for(int i=0; i<str.length();i++)

              Boolean flag = Character.isDigit(str.charAt(i));

                 System.out.println("'"+str.charAt(i)+"' is a number");
                 System.out.println("'"+str.charAt(i)+"' is not a number");


          //To check first character is a number or not ?

              Boolean flag2 = str.substring(0, 1).matches("[0-9]");

                 System.out.println("First Character is a number..!");
                 System.out.println("First character is not a number..!");



‘j’ is not a number
‘a’ is not a number
‘v’ is not a number
‘a’ is not a number
‘4’ is a number
‘s’ is not a number
First character is not a number..!


You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


5 Responses to “Check the Character/First Character Of a String is Number or Not in Java”
  1. There is a lot of very useful knowledge in your post to help me solve problems.I enjoy reading your post and hope to see more.

  2. venkatesh says:

    when to use equals() method and compareTo() method can u explain.

  3. suseela says:

    i couldn’t found multi thread concept in (core java) java4s

  4. vidya says:

    what is difference between string,string buffer,string builder concepts

  5. Jamal Mirza says:

    There is lot of materials regarding Java as well as many technology i started to learn and practice from Java4s it is awesome blog.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.