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
9,081 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,190 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,920 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,140 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
2,683 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, 2019 in Java by Omkar
• 69,220 points
11,099 views
0 votes
1 answer

Is it possible to upload a file from desktop to web using Selenide?

Hi, @Nikhil, Yes it's possible to drop a ...READ MORE

answered Dec 16, 2020 in Java by Gitika
• 65,770 points
1,067 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 Aug 1, 2018 in Java by samarth295
• 2,220 points
5,182 views
+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 81,223 views
+1 vote
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,500 points
8,359 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,500 points
17,433 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,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,876 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,190 points
794 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, 2019 in Java by Sirajul
• 59,230 points
3,471 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP