Hello guys, welcome to lingarajtechhub.com. Today we are going to discuss input a character and check alphabet digit special characters in java.
Code:
package com.Special_Character.java; import java.util.Scanner; public class Character { public static void main(String[] args) { char ch; int a; Scanner sc=new Scanner(System.in); System.out.println("Enter character:"); ch=sc.next().charAt(0); if((ch>='a'&& ch<='z')||(ch>='A'&& ch<='Z')) { System.out.println(ch+":it is a Alphabet"); } else if(ch>='0' && ch<='9') { System.out.println(ch+":it is a digit"); } else if((ch=='*')||(ch=='$')||(ch=='#')||(ch=='@')) { System.out.println(ch+":Special character"); } else System.out.println("There is no alphabet , digit & special character"); } }
Line-1:
- package com.Special_Character.java
It is a java package , this package is created in a java project and it contains classes inside the package.
Line-2:
- import java.util.Scanner
In java, Scanner is a class. util package used for obtaining the input of the data types like int, double, float , char and strings.
Line-3:
- public class Character
“Character” is the class-name , class is a template used to create objects & class-name first letters must be capital. It is public means we can use this outside of the package.
Line-4:
- public static void main(String[] args)
It is a main method in which we can execute a program.
- public : It is an access modifier. The main() is public because we can access main() outside of the class , if main() can’t be public then JVM(Java Virtual Machine) can’t be called main() & code can’t be executed.
- Static : static is a keyword in which the main method has to be static in which JVM can load the class into memory and call the main method without creating an instance.
- Void : Void is a data type. It means no return type.
- main(): Main is a function.
- String [] args : string means collection of characters ,String[] args represents a collection of Strings that are separated by a space and can be typed into the program on the terminal directly.
Line-5:
- char ch;
Here “ch” is the variable and char is a data type of the variable.
Line-6:
- int a;
Here “a” is the variable and int is a data type of the variable.
Line-7:
- Scanner sc=new Scanner(System.in);
Scanner is a class and sc is the instance of the object which is the new Scanner . System.in is an argument.
Line-8:
- System.out.println(“Enter character:”);
System is a class name and out is the instance of the system. Println is to print the new line.
Line-9:
- ch=sc.next().charAt(0);
ch is a variable that we take and sc is our instance name which is created for an object and next() is a method of a Scanner object.The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.
Line-10 to Line-23:
- if((ch>=’a’&& ch<=’z’)||(ch>=’A’&& ch<=’Z’)) {
System.out.println(ch+”:it is an Alphabet”);
} else if(ch>=’0′ && ch<=’9′) {
System.out.println(ch+”:it is a digit”);
} else if((ch==’*’)||(ch==’$’)||(ch==’#’)||(ch==’@’)) {
System.out.println(ch+”:Special character”);
} else
System.out.println(“There is no alphabet , digit & special character”);
Here we used the “Else-If” condition .It is going to check the logic if the logic is true then it prints the statement, if the “if block” condition is false then it checks the “ else if” block then it checks the “else” block and shows the output.