• To check if first character of a string is a number or not in java we can check in multiple ways.
  • By using Character.isDigit() method by passing character of string using for loop or substring method.
  • Other way is to check str.substring(0, 1).matches("[0-9]").
  • Lets see an example program in java to check first character of a string is digit or character. 



Program #1: Java example program to check the each character of string is number or not   character is a letter or number in Java without using regexes?

  1. package StringInterviewprograms;
  2. /**
  3.  * Check whether the given character is a number /digit or not by using java character isdigit
  4.  *method
  5.  * @author www.instanceofjava.com
  6.  */
  7. public class CheckEachCharacterNumberOrNot {
  8.     public static void main(String[] args) {
  9.         String str = "instanceofjava37";
  10.  
  11.         //Check whether the given character is a number /digit or not ?
  12.  
  13.         for(int i=0; i<str.length();i++)
  14.         {
  15.             Boolean flag = Character.isDigit(str.charAt(i));
  16.  
  17.             if(flag){
  18.  
  19.                System.out.println("'"+str.charAt(i)+"' is a number");
  20.             }
  21.             else{
  22.                System.out.println("'"+str.charAt(i)+"' is not a number");
  23.             }
  24.  
  25.         }
  26.         
  27. }
  28.  
  29. }

Output:

  1. 'i' is not a number
  2. 'n' is not a number
  3. 's' is not a number
  4. 't' is not a number
  5. 'a' is not a number
  6. 'n' is not a number
  7. 'c' is not a number
  8. 'e' is not a number
  9. 'o' is not a number
  10. 'f' is not a number
  11. 'j' is not a number
  12. 'a' is not a number
  13. 'v' is not a number
  14. 'a' is not a number
  15. '3' is a number
  16. '7' is a number
     
Program #2: Java example program to check the First character of string is number or not by using regex.

  1. package StringInterviewprograms;
  2. /**
  3.  * Check whether the given character is a number /digit or not
  4.  * @author www.instanceofjava.com
  5.  */
  6. public class CheckFirstCharacterNumberOrNot {
  7.  
  8.     public static void main(String[] args) {
  9.         String str = "instanceofjava37";
  10.  
  11.           Boolean flag1 = str.substring(0, 1).matches("[0-9]");
  12.  
  13. if(flag1){
  14.  
  15.  System.out.println("First Character is a number..!");
  16.  
  17.  }
  18.  else{
  19.    System.out.println("First character is not a number..!");
  20. }
  21. }
  22.  
  23. }

Output:

  1. First character is not a number..!


Program #3: Java example program to check the First character of string is number or not using eclipse IDE.


Check first character string number digit java

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu