Is it possible to take char input from scanner?

0 votes
I am trying to find a way to take a char input from the keyboard.

Any help?
Apr 27, 2018 in Java by sophia
• 1,400 points
2,229 views

3 answers to this question.

0 votes

To take input as exactly one character you could use:

char c = reader.findInLine(".").charAt(0);

answered Apr 27, 2018 by Akrati
• 3,150 points
0 votes

You could take the first character from Scanner.next

char c = reader.next().charAt(0);

To consume exactly one character you could use

char c = reader.findInLine(".").charAt(0);

To consume strictly one character you could use

char c = reader.next(".").charAt(0);
answered Aug 6, 2018 by Sushmita
• 6,880 points

What is the difference between exactly and strictly.  In what case do the last 2 methods behave differently?

When you use exactly, the reader will take only the first character, irrespective of how many characters you input. 

For example:

import java.util.Scanner;  

public class ReaderExample {  

    public static void main(String[] args) {  

        try {  

        Scanner reader = new Scanner(System.in);

        char c = reader.findInLine(".").charAt(0);

            reader.close();  

            System.out.print(c);

        } catch (Exception ex) {  

            System.out.println(ex.getMessage());  

        }

     

    }  

}  

When you give a set of characters as input, say "abcd", the reader will consider only the first character i.e., the letter 'a'

But when you use strictly, the input should be just one character. If the input is more than one character, then the reader will not take the input

import java.util.Scanner;  

public class ReaderExample {  

    public static void main(String[] args) {  

        try {  

        Scanner reader = new Scanner(System.in);

        char c = reader.next(".").charAt(0);

            reader.close();  

            System.out.print(c);

        } catch (Exception ex) {  

            System.out.println(ex.getMessage());  

        }

      

    }  

}  

Suppose you give input "abcd", no input is taken, and the variable c will have Null value.

0 votes
// Use a BufferedReader to read characters from the console.
import java.io.*;
class BRRead {
public static void main(String args[]) throws IOException
{
char c;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
// read characters
do {
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}
}
answered Sep 7, 2018 by Daisy
• 8,050 points

Related Questions In Java

0 votes
1 answer

Is it possible to run a java program from command line on windows?How?

  Let's say your file is in C:\myprogram\ Run ...READ MORE

answered Apr 18, 2018 in Java by sophia
• 1,400 points
210 views
0 votes
1 answer

Is there a way to mask the password from java user input that comes from Scanner class and System.out.Println()?

You can use the console class to do so. ...READ MORE

answered May 27 in Java by Omkar
• 68,480 points
1,514 views
0 votes
3 answers

How to read input from Console using Scanner Class?

A simple example: import java.util.Scanner; public class Expl { ...READ MORE

answered Jul 31, 2018 in Java by samarth295
• 2,190 points
464 views
0 votes
1 answer

Is it possible to create a memory leak with Java

Here's a good way to create a memory ...READ MORE

answered May 30, 2018 in Java by parth295
• 4,640 points
659 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 11,460 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,430 points
2,442 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,430 points
6,309 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,430 points
803 views
0 votes
1 answer

How to take input from users?

Scanner Class is used to take input ...READ MORE

answered Apr 17, 2018 in Java by Akrati
• 3,150 points
41 views
0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14 in Java by Sirajul
• 41,000 points
137 views