In this article we will show you the solution of how to take char input in java, the nextInt() method of the Java Scanner class reads integer values, the nextDouble() method reads double values, the nextLong() method reads long values, etc.
Nevertheless, reading a Java character is not possible using the nextChar() method of the Scanner class.
In Java, we use the next() function from the Scanner class, then chatAt() from the String class to read a character.
A method of the Java Scanner class is next(). The next whole token found by this scanner is found and returned.
Input that complies with the delimiter pattern comes before and comes after a complete token.
Even if a previous call to hasNext() returned true, it can still block while expecting for input to scan.
The Java scanner class has nextInt(), nextLong(), nextDouble(), and more functions. However nextChar does not exist (). We will now discuss the Java idea of taking char input.
Step By Step Guide On How To Take Char Input In Java :-
The Java String class's charAt() function is used. It returns the character value located at the requested index (between 0 and length()-1).
import java.util.Scanner; public class CharInputEx { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Input a character: "); String str=sc.next(); char ch=str.charAt(0); System.out.println("You have entered "+ch); } }
- As you can see, we have written a Java programme that accepts a character from the user as input & prints that same character.
- We import the package titled import java.util.Scanner at the beginning of the code.
- That merely denotes importing the Scanner classes from the parent directory java's util directory.
- Many Java classes have a java. util. Scanner importing declaration at the top, which indicates that the Scanner class has been utilized somewhere in the code.
- Then, we begin the main method, which is a member of the public class CharinputEx.
- The main method, which is public static void, is then called.
- After that, you can use any of the methods listed inside the Scanner classes documentation by creating an instance of the class and calling the appropriate method.
- After that, we print the statement that entered a character using System.out.println.
- The next() function of the Java scanner class is then utilised. Text with tokens is returned by the next() method. As a default delimiter, the next() method utilises whitespace.
- Next, we employed the charAt method. The character in a string at the given index is returned by the charAt() function.
- In the final section, we once more print a sentence using system.out.println.
Conclusion :-
As a result, we have successfully learned how to take char input in Java. We also learned that the charAt() method returns the character that is located at the supplied index in a string.
And in Java, we use the chatAt() method from the String class followed by the next() method from the Scanner class to read a character.
I hope this article on how to take char input in java helps you and the steps and method mentioned above are easy to follow and implement.