The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to check if first character of a string is a number or not in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
How to check if first character of a string is a number or not in java Posted: May 6, 2017 1:48 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: How to check if first character of a string is a number or not in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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

Read: How to check if first character of a string is a number or not in java

Topic: 40% off Dell Computer Ultrasharp U2415 24.0-Inch Screen LED Monitor - Deal Alert Previous Topic   Next Topic Topic: Java Nio Tutorial for Beginners

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use