How To Take Character Input In Java Using Scanner Class
Last Updated : Mar 11, 2024
IN - Java | Written & Updated By - Amruta
In this article we will show you the solution of how to take character input in java using scanner class, the first package will imported by eclipse default during creation of each java program at initial stage. The utility class supports scanner class to request input from user.
After successful scanner object creation you can take any type of input from user with few statement or property.
Step By Step Guide On How To Take Character Input In Java Using Scanner Class :-
The scanner class is part of the java.util package and it was received from user then parse into primitive data types such as float, int, double or string.
The main class named as ‘ChrInp_prgm’ and there we provide main method definition. Within that you need to use next() and charAt() methods to achieve the result and displayed on terminal.
//GET CHARACTER INPUT FROM USER package java_prgms; import java.util.*; class ChrInp_prgm { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter Character"); char c=sc.next().charAt(0); System.out.println("You input character is : "+c); } }
- The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
- First we defined main class ‘ChrInp_prgm’, there we provide static main method definition. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
- Inside main method, we creating scanner object ‘sc’ with prebuilt rule based. Otherwise that will throw error then we printed text ‘Enter Character’ to request user for character input.
- The next() method will returns the next string in text and the charAt(0) defines and returns first character of string. In that case, if you gives string input then it will takes only first character of string.
- To collecting character you need to appends the scanner object ‘sc’ with next() and charAt(0). And returning result stored on variable ‘c’ and displayed final result online with text ‘You input character is : g’ by println() method.
- The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
- Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.
Conclusion :-
In conclusion now we are able to know how to take character input in java through scanner class.
To compile java program you need to tell the java compiler which program to compile through ‘javac ChrInp_prgm.java’.
After successful compilation you need to execute ‘java ChrInp_prgm‘ line for gets the output.
Then you can see output ' Enter Character’ now you need enter input for example you gave letter ‘a’ then it displays final result ‘You entered input is : a'.
On eclipse just press run button then you will get output inside terminal.
I hope this article on how to take character input in java using scanner class helps you and the steps and method mentioned above are easy to follow and implement.