Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a single char in Java

Tags:

java

How can a char be entered in Java from keyboard?

like image 921
dato datuashvili Avatar asked Jun 15 '10 07:06

dato datuashvili


People also ask

How do you read a single character?

Read Single Character using Scanf() from User in C So, to read a single character from console, give the format and argument to scanf() function as shown in the following code snippet. char ch; scanf("%c", ch); Here, %c is the format to read a single character and this character is stored in variable ch .

How do you read a single character from a scanner class?

Reading a character using the Scanner class But, you still can read a single character using this class. The next() method of the Scanner class returns the next token of the source in String format. This reads single characters (separated by delimiter) as a String.

How do you check a single character in a string?

You can use string. indexOf('a') . If the char a is present in string : it returns the the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

How do I find a specific character in a string in Java?

Java String indexOf() Method The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.


5 Answers

You can either scan an entire line:

Scanner s = new Scanner(System.in);
String str = s.nextLine();

Or you can read a single char, given you know what encoding you're dealing with:

char c = (char) System.in.read();
like image 200
Yuval Adam Avatar answered Sep 22 '22 20:09

Yuval Adam


You can use Scanner like so:

Scanner s= new Scanner(System.in);
char x = s.next().charAt(0);

By using the charAt function you are able to get the value of the first char without using external casting.

like image 27
SolarBear Avatar answered Sep 26 '22 20:09

SolarBear


Using nextline and System.in.read as often proposed requires the user to hit enter after typing a character. However, people searching for an answer to this question, may also be interested in directly respond to a key press in a console!

I found a solution to do so using jline3, wherein we first change the terminal into rawmode to directly respond to keys, and then wait for the next entered character:

var terminal = TerminalBuilder.terminal()
terminal.enterRawMode()
var reader = terminal.reader()

var c = reader.read()
<dependency>
    <groupId>org.jline</groupId>
    <artifactId>jline</artifactId>
    <version>3.12.3</version>
</dependency>
like image 23
drcicero Avatar answered Sep 23 '22 20:09

drcicero


You can use a Scanner for this. It's not clear what your exact requirements are, but here's an example that should be illustrative:

    Scanner sc = new Scanner(System.in).useDelimiter("\\s*");
    while (!sc.hasNext("z")) {
        char ch = sc.next().charAt(0);
        System.out.print("[" + ch + "] ");
    }

If you give this input:

123 a b c x   y   z

The output is:

[1] [2] [3] [a] [b] [c] [x] [y] 

So what happens here is that the Scanner uses \s* as delimiter, which is the regex for "zero or more whitespace characters". This skips spaces etc in the input, so you only get non-whitespace characters, one at a time.

like image 26
polygenelubricants Avatar answered Sep 22 '22 20:09

polygenelubricants


i found this way worked nice:

    {
    char [] a;
    String temp;
    Scanner keyboard = new Scanner(System.in);
    System.out.println("please give the first integer :");
    temp=keyboard.next();
    a=temp.toCharArray();
    }

you can also get individual one with String.charAt()

like image 44
Marc We Avatar answered Sep 23 '22 20:09

Marc We