Check String for Spaces in Java Program – In this specific article, we will be dealing with the code here to check whether a given string has a white space using Java language with suitable examples and sample output.
- The problem here is to check whether a user-defined string has a whitespace present.
- Our constraint here is the inability to return a positive result if the only white space present is the last character in a string.
- The input here is a user-defined string with either space present or absent.
- The output is a boolean value of true or false printed out which helps determine the presence of whitespaces in java string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; class CheckSpace { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the string"); String s = sc.nextLine(); System.out.println("Is string ("+s+") has space :"+hasSpace(s)); } static boolean hasSpace(String str) { for(int i=0;i<str.length();i++) { if(str.charAt(0)==' ' || str.charAt(i)==' '&&str.charAt(i+1)!=' ') { return true; } } return false; } } |
Output – 1:
Solution:
- In the main method, a new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java.
- Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class while the Java program is used to find space in a string.
- The next line prints a statement to the console instructing the user to enter a string.
- The .nextLine() method is called by the Scanner object sc. This method reads a line of user input of a string value and stores it to the string variable s.
- Within the print statement in the last line, the hasSpace method is called and string s is passed in as the argument.
123456789static boolean hasSpace(String str) {for(int i=0;i<str.length();i++) {if(str.charAt(0)==' ' || str.charAt(i)==' '&&str.charAt(i+1)!=' ') {return true;}}return false;}
- In this method, the loop iterates over all the characters of the string (since i<str.length()) unless it finds a white-space midway and ends the method implementation.
- The parameter string str represents the string input by the user and is passed in as the argument s from the main method.
- As the loop iterates, the conditional statement checks certain conditions using OR operator ( || ) and AND operator ( && ).
- If the first character of the string i.e. the character at index 0 is a white-space, the condition is satisfied OR if the character at any index i is a white-space AND the character following it at index i+1 is not a space, the condition is satisfied.
- The boolean value true is returned and printed out if the condition is satisfied and value false is returned and printed out otherwise.
1 2 3 |
Enter the string hello world Is string (hello world) has space :true |
- Here, the input hello world has a white-space at index 5, and hence the conditional statement in the method hasSpace is satisfied and returns and prints the boolean value true.
Output – 2:
1 2 3 |
Enter the string "" Is string ("") has space :false |
- Here, the input ” “ has no whitespaces, and hence the conditional statement in the method hasSpace is not satisfied and returns and prints the boolean value false.