×

Java Programs

Java Practice

Java program to check a given character is a whitespace character or not without using the built-in library method

Given/input a character, we have to check a given character is a whitespace character or not without using the built-in library method.
Submitted by Nidhi, on February 24, 2022

Problem statement

In this program, we will create 3-character variables and check characters are whitespace characters or not without using any built-in library method.

Java program to check a given character is a whitespace character or not

The source code to check a given character is a whitespace character or not without using the built-in library method is given below. The given program is compiled and executed successfully.

// Java program to check a given character is a 
// whitespace character or not without using 
// the built-in library method

import java.util.Scanner;

public class Main {
  static boolean isWhiteSpace(char ch) {
    if ((ch == ' ') || (ch == '\t') || (ch == '\n') || (ch == 'v') || (ch == '\r') || (ch == '\f'))
      return true;
    return false;
  }

  public static void main(String[] args) {
    char ch1 = ' ';
    char ch2 = '\t';
    char ch3 = 't';

    if (isWhiteSpace(ch1))
      System.out.printf("Given character is a whitespace character\n");
    else
      System.out.printf("Given character is not a whitespace character\n");

    if (isWhiteSpace(ch2))
      System.out.printf("Given character is a whitespace character\n");
    else
      System.out.printf("Given character is not a whitespace character\n");

    if (isWhiteSpace(ch3))
      System.out.printf("Given character is a whitespace character\n");
    else
      System.out.printf("Given character is not a whitespace character\n");
  }
}

Output

Given character is a whitespace character
Given character is a whitespace character
Given character is not a whitespace character

Explanation

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains two static methods main() and isWhiteSpace().

The isWhiteSpace() method returns a Boolean value based on input character, it returns true if a given character is a whitespace character otherwise it returns false.

The main() method is an entry point for the program. Here, we created 3-character variables and check created variables contains a whitespace character or not and printed the appropriate message.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.