How to check if a character is a special character in java

  • To check if string contains special character or not or  check whether first character is special character or not we can use regex or contains method of sting class.
  • By using  java.util.regex.Matcher class and java.util.regex.Pattern class methods we can check whether string has special character or not
  • Lets see an example program on how to check a string has special character or not using java regex.



Program #1: Java example program to check string has special character or not using  java regex's?
 
  1. package StringInterviewprograms;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class CheckSpecialCharacterString {
  6.  
  7.     /**
  8.      * Check whether the String contains special character or not using java
  9.      * @author www.instanceofjava.com
  10.      */
  11.     
  12. public static void main(String[] args) {
  13.  
  14. String Str="Java String interview questions ";
  15. String Str1="Java String interview questions % ";
  16. Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
  17. Matcher m = p.matcher(Str);
  18. boolean check = m.find();
  19.  
  20. if (check)
  21.    System.out.println("String: "+Str+" contains special character");
  22. else
  23.    System.out.println("String: "+Str+" does not contains any special character");
  24.  
  25. Matcher m1= p.matcher(Str1);
  26.  
  27. boolean flag = m1.find();
  28.  
  29. if(flag)
  30.        System.out.println("String: "+Str1+" contains special character");
  31.     else
  32.        System.out.println("String: "+Str1+" does not contains any special character");
  33.  
  34.    }
  35. }
Output:

  1. String: Java String interview questions  does not contains any special character
  2. String: Java String interview questions %  contains special character

  • We can check each character of a string is special character or not without using java regex's.
  • By using String class contains method and for loop we can check each character of a sting is special character or not.
  • Let us see a java example program on how to check each character (including space) of a string is special character or not.
  • String validation for special characters.


Program #2: Java example program to check each character of string is special character or not without using  java regex's?

  1. package StringInterviewprograms;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class CheckSpecialCharacterString {
  7.  
  8.     /**
  9.      * Check whether the each character of String is special character or not using java
  10.      * @author www.instanceofjava.com
  11.      */
  12.     
  13. public static void main(String[] args) {
  14. String Str="Java String interview questions*$%";
  15.   
  16. String specialCharacters=" !#$%&'()*+,-./:;<=>?@[]^_`{|}";
  17.  
  18. for (int i = 0; i < Str.length(); i++) {
  19.     
  20.     if (specialCharacters.contains(Character.toString(Str.charAt(i))))
  21.     {
  22.        
  23.         System.out.println(Str.charAt(i)+": is a special character");
  24.     }  
  25.   }
  26.  
  27. }
  28.  
  29. }

Output:


  1.  : is a special character
  2.  : is a special character
  3.  : is a special character
  4. *: is a special character
  5. $: is a special character
  6. %: is a special character

Program #3: Java example program to check each character of string is special character or not without using  java regex's Using Eclipse IDE.


Check Special Character String java

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

  • 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

Java program to Get current date and time

  • By using date and calendar classes we can get current date and time in java.
  • By using SimpleDateFormat we can convert date into string format.
  • Create object of date.
  • Create object of SimpleDateFormat by passing required format to constructor
  • Format using object of SimpleDateFormat 




Using Date Object:


Java Program to get current date and time using Date object and SimpleDateFormat


  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4.  
  5. public class GetDateTime {
  6.  
  7.     /**
  8.      * Get current date and time in java
  9.      * @author www.instanceofjava.com
  10.      */
  11.     public static void main(String[] args) {
  12.         
  13.         
  14.         DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  15.         Date date = new Date();
  16.         System.out.println(df.format(date));
  17.  
  18. }
  19. }

Output:


  1. 23-04-2017 19:43:23
Using Calendar Object:

Java Program to get current date and time using Calendar object and SimpleDateFormat


  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4.  
  5. public class GetDateTime {
  6.  
  7.     /**
  8.      * Get current date and time in java
  9.      * @author www.instanceofjava.com
  10.      */
  11.  public static void main(String[] args) {
  12.         
  13.         
  14.         DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  15.         Calendar calendarobj = Calendar.getInstance();
  16.         System.out.println(df.format(calendarobj.getTime()));
  17.  }
  18.  
  19. }

Output:


  1. 23-04-2017 19:47:45
Java Program to get current date and time in mill seconds


get current date and time java

Java program to find maximum of two numbers using Math.max()

  • We can find maximum of two numbers by using if condition check.
  • In java.lang.Math class also having a method Math.max() which will take two numbers and returns maximum number.
  • We have separate methods for int , float , double and long.




  1.  public static double max(double a, double b)

Java program to find maximum of two double values using Math.max() method.


  1. package com.mathmax;
  2. public class MathMAx {
  3.     
  4.     /**
  5.      * Math.max() method in java
  6.      * @author www.instanceofjava.com
  7.      */
  8.  
  9.   public static void main(String[] args) {
  10.         
  11.         
  12.         double a = 234567;
  13.         double b =234557;
  14.      
  15.         double x = 764554;
  16.         double y =764464;
  17.         
  18.         System.out.println("Math.max(" + a + "," + b + ")=" + Math.max(a, b));
  19.         System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y));
  20.         
  21.     }
  22.  
  23. }
Output:

  1. Math.pow(3.0,4.0)=81.0
  2. Math.pow(4.0,3.0)=64.0
  3. Math.pow(4,2)=16.0

Java program to find maximum of two float values using Math.max() method.



math.max method in java float


Java program to find maximum of two int values using Math.max() method.
 
  1. package com.mathmax;
  2. public class MathMAx {
  3.     
  4.     /**
  5.      * Math.max() method in java
  6.      * @author www.instanceofjava.com
  7.      */
  8.  
  9. public static void main(String[] args) {
  10.         
  11.         System.out.println( Math.max(1, 1));
  12.         System.out.println( Math.max(1, 2));
  13.         System.out.println(Math.max(1.222f, 1.223f));
  14.         System.out.println(Math.max(1.222, 1.223));
  15.         
  16.     }
  17.  
  18. }

Output:

  1. 1
  2. 2
  3. 1.223
  4. 1.223

Find power using math.pow() in java

  • java.lang.Math.pow() method used to find the power of numbers.
  • It is a static method define in Math class so that we can call directly using classname.
  • Math.pow();
  • It takes two double arguments and return type is double.

  1. public final class Math extends Object
  1. public static double pow(double a, double b)



Java Program to explain the usage of Math.pow() method in java

  1. public class MathPow {
  2.  
  3.     /**
  4.      * Math.pow() method in java
  5.      * @author www.instanceofjava.com
  6.      */
  7. public static void main(String[] args) {
  8.     
  9.     
  10.     double a = 3.0;
  11.     double b = 4.0;
  12.  
  13.     
  14.     System.out.println("Math.pow(" + a + "," + b + ")=" + Math.pow(a, b));
  15.     System.out.println("Math.pow(" + b + "," + a + ")=" + Math.pow(b, a));
  16.    
  17.     System.out.println("Math.pow(4,2)="+Math.pow(4,2));
  18. }
  19.  
  20. }

Output:


  1. Math.pow(3.0,4.0)=81.0
  2. Math.pow(4.0,3.0)=64.0
  3. Math.pow(4,2)=16.0
math pow method in java

Math.round() method in java

  • By using java.lang.math.round in java we will get crossest numbers.
  • If we pass double number with some decimal points it will returns closest integer or long  number. 
  • If we pass float number with some decimal points it will return closest integer or long number.
  • Lets see an example program on how to use math.random() method.
  • Math.random() method will returns closest int or long numbers.



Java program to round double number using math.random() method in java


  1. package com.mathrandommethod;
  2.  
  3. public class MathRandom {
  4.         /**
  5.          * Math.random() in java
  6.          * @author www.instanceofjava.com
  7.          */
  8.     public static void main(String[] args) {
  9.         
  10.           float float1 = 23.34f;
  11.           float float2 = 23.623f;
  12.           double double1 = 234.433;
  13.          double double2 = 234.654;
  14.  
  15.           System.out.println(Math.round(float1));
  16.           System.out.println(Math.round(float2)); 
  17.           System.out.println(Math.round(double1)); 
  18.           System.out.println(Math.round(double2));
  19.  }
  20.  
  21. }
Output:

  1. 23
  2. 24
  3. 234
  4. 235
java math random

Select Menu