×

Java Programs

Java Practice

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

Given a character, we have to check whether it is a printable character or not without using the built-in library method.
Submitted by Nidhi, on February 25, 2022

Problem statement

In this program, we will create three character variables and check they contain a printable character or not, and print the appropriate message.

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

The source code to check a given character is a printable 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 printable character 
// or not without using the built-in library method

public class Main {

  static boolean isPunctuation(char ch) {
    if (ch == '!' || ch == '\"' || ch == '#' || ch == '$' || ch == '%' || ch == '&' || ch == '\'' || ch == '(' || ch == ')' || ch == '*' || ch == '+' || ch == ',' || ch == '-' || ch == '.' || ch == '/' || ch == ':' || ch == ';' || ch == '<' || ch == '=' || ch == '>' || ch == '?' || ch == '@' || ch == '[' || ch == '\\' || ch == ']' || ch == '^' || ch == '`' || ch == '{' || ch == '|' || ch == '}')
      return true;
    return false;
  }

  static boolean isAlphaNumeric(char ch) {
    if ((ch >= '0' & ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
      return true;
    return false;
  }

  static boolean isPrintable(char ch) {
    if (isAlphaNumeric(ch) || isPunctuation(ch))
      return true;
    return false;
  }

  public static void main(String[] args) {
    char ch1 = 'a';
    char ch2 = 'A';
    char ch3 = 95;

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

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

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

Output

Given character is a printable character
Given character is a printable character
Given character is not a printable character

Explanation

In the above program, we created a public class Main. It contains four static methods main(), isPunctuation(), isAlphanumeric(), and isPrintable().

The isPunctuation() method returns a Boolean value based on input character, it returns true if the given character is a punctuation mark otherwise it returns false.

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

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

The main() method is an entry point for the program. Here, we created three variables with some initial values. Then we checked variables containing a printable character or not and printed appropriate messages.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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