Q:

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

belongs to collection: Java Basic Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will read a character from the user and check the input character is alphanumeric or not without using any built-in library method.

Program/Source Code:

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

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

import java.util.Scanner;

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

  public static void main(String[] args) {
    Scanner X = new Scanner(System.in);

    char ch = 0;

    System.out.printf("Enter character: ");
    ch = X.next().charAt(0);

    if (isAlphaNumeric(ch))
      System.out.printf("Given character is an alphanumeric character\n");
    else
      System.out.printf("Given character is not an alphanumeric character\n");
  }
}

Output:

RUN 1:
Enter character: K
Given character is an alphanumeric character

RUN 2:
Enter character: 8
Given character is an alphanumeric character

RUN 3:
Enter character: %
Given character is not an alphanumeric character

Explanation:

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

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

The main() method is an entry point for the program. Here, we read a character from the user and checked input character is alphanumeric or not and printed the appropriate message.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to check a given character is a digit... >>
<< Java program to find the remainder without using m...