Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

check if input is empty java

Program to check if the String is Empty in Java
Last Updated : 22 Jan, 2020
Given a string str, the task is to check if this string is empty or not, in Java.

Examples:

Input: str = "" 
Output: True

Input: str = "GFG"
Output: False
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach:

Get the String to be checked in str
We can simply check if the string is empty or not using the isEmpty() method of String class
Syntax:
if (str.isEmpty())
Print true if the above condition is true. Else print false.
Below is the implementation of the above approach:


// Java Program to check if
// the String is empty in Java
  
class GFG {
  
    // Function to check if the String is empty
    public static boolean isStringEmpty(String str)
    {
  
        // check if the string is empty or not
        // using the isEmpty() method
        // and return the result
        if (str.isEmpty())
            return true;
        else
            return false;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String str1 = "GeeksforGeeks";
        String str2 = "";
  
        System.out.println("Is string "" + str1
                           + "" empty? "
                           + isStringEmpty(str1));
        System.out.println("Is string "" + str2
                           + "" empty? "
                           + isStringEmpty(str2));
    }
}
Output:
Is string "GeeksforGeeks" empty? false
Is string "" empty? true
Comment

PREVIOUS NEXT
Code Example
Java :: show all spring boot beans 
Java :: change brightness of image in java 
Java :: java use space in enums 
Java :: resultset next method 
Java :: how to add multiple action listeners java swing 
Java :: retrofit post body 
Java :: Java Access ConcurrentHashMap Elements 
Java :: java empty array list vs null elements 
Java :: java program to search item from name in class 
Java :: java resultset to table 
Java :: can the method local inner class object access method local variables 
Java :: Java TreeMap Example NavigableMap 
Java :: bool in java 
Java :: fix nullpointerexception 
Java :: java nested static class 
Java :: how to find mongo java driver version 
Java :: java string strip 
Java :: get distinct values from list of objects java 
Java :: java encapsulation 
Java :: java 14 switch 
Java :: lcm of two number in java 
Java :: room insert and return id 
Java :: Java Add elements to a HashMap 
Java :: each loop in java 
Java :: java comment 
Java :: spring mvc 
Java :: find subarray with given sum 
Java :: java convert int to string 
Java :: run java class file 
Java :: android sqlite insert or replace 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =