The syntax to input a character through scanner class.
Syntax: char = .nextLine();
Syntax: char = .next().charAt(0);
Syntax: char = .charAt(0);
Syntax: char = .next().charAt(1);
Answers
Answered by
2
Explanation:
To read a char, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) function returns the first character in that string.
// Java program to read character using Scanner
// class
import java.util.Scanner;
public class ScannerDemo1
{
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// Character input
char c = sc.next().charAt(0);
// Print the read value
System.out.println("c = "+c);
}
}
Input : g
Output : c=g
Similar questions