14

What is the Scanner method to get a char returned by the keyboard in Java.

like nextLine() for String, nextInt() for int, etc.

skaffman
  • 381,978
  • 94
  • 789
  • 754
bizarrechaos
  • 171
  • 1
  • 2
  • 8

6 Answers6

20

To get a char from a Scanner, you can use the findInLine method.

    Scanner sc = new Scanner("abc");
    char ch = sc.findInLine(".").charAt(0);
    System.out.println(ch); // prints "a"
    System.out.println(sc.next()); // prints "bc"

If you need a bunch of char from a Scanner, then it may be more convenient to (perhaps temporarily) change the delimiter to the empty string. This will make next() returns a length-1 string every time.

    Scanner sc = new Scanner("abc");
    sc.useDelimiter("");
    while (sc.hasNext()) {
        System.out.println(sc.next());
    } // prints "a", "b", "c"
polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
  • @poly : default Delimiter would be (`" "`)...? how to set that back? is this the suggested way -asking because this one is hell easy then all other given solutions on SO!! – NoobEditor Jun 20 '14 at 11:40
  • `findInLine(".")`will not match newlines. The second approach does. – user3669782 Aug 07 '15 at 18:45
  • why use `findInLine(".");` I can get input by this `Scanner in =new Scanner(System.in); char a; a=in.next().charAt(0); System.out.println(a);` – Asif Mushtaq Sep 27 '15 at 09:24
4

You can use the Console API (which made its appearance in Java 6) as follows:

Console cons = System.console();
if(cons != null) {
  char c = (char) cons.reader().read();  // Checking for EOF omitted
  ...
}

If you just need a single line you don't even need to go through the reader object:

String s = cons.readLine();
Itay Maman
  • 28,289
  • 9
  • 76
  • 114
2

Java's Scanner class does not have a built in method to read from a Scanner character-by-character.

http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

However, it should still be possible to fetch individual characters from the Scanner as follows:

Scanner sc:

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

And you could use it to fetch each character in your scanner like this:

while(sc.hasNext()){
    char c = sc.findInLine(".").charAt(0);
    System.out.println(c); //to print out every char in the scanner
}

The findInLine() method searches through your scanner and returns the first String that matches the regular expression you give it.

eipxen
  • 218
  • 1
  • 9
0
Console cons = System.console();

The above code line creates cons as a null reference. The code and output are given below:

Console cons = System.console();
if (cons != null) {
    System.out.println("Enter single character: ");
    char c = (char) cons.reader().read();
    System.out.println(c);
}else{
    System.out.println(cons);
}

Output :

null

The code was tested on macbook pro with java version "1.6.0_37"

j0k
  • 21,914
  • 28
  • 75
  • 84
0

sc.next().charat(0).........is the method of entering character by user based on the number entered at the run time

example: sc.next().charat(2)------------>>>>>>>>

0
Scanner sc = new Scanner (System.in)
char c = sc.next().trim().charAt(0);
Raidri supports Monica
  • 15,886
  • 7
  • 49
  • 57