All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Take Char Input In Java

Last Updated : Mar 11, 2024

How To Take Char Input In Java

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);
}
}
  1. As you can see, we have written a Java programme that accepts a character from the user as input & prints that same character.
  2. We import the package titled import java.util.Scanner at the beginning of the code.
  3. That merely denotes importing the Scanner classes from the parent directory java's util directory.
  4. 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.
  5. Then, we begin the main method, which is a member of the public class CharinputEx.
  6. The main method, which is public static void, is then called.
  7. 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.
  8. After that, we print the statement that entered a character using System.out.println.
  9. 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.
  10. Next, we employed the charAt method. The character in a string at the given index is returned by the charAt() function.
  11. 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.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪