在 Java 中读取单个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3043306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading a single char in Java
提问by dato datuashvili
How can a char be entered in Java from keyboard?
如何从键盘在 Java 中输入字符?
采纳答案by Yuval Adam
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
,前提是您知道要处理的编码:
char c = (char) System.in.read();
回答by user357206
Maybe you could try this code:
也许你可以试试这个代码:
import java.io.*;
public class Test
{
public static void main(String[] args)
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String userInput = in.readLine();
System.out.println("\n\nUser entered -> " + userInput);
}
catch(IOException e)
{
System.out.println("IOException has been caught");
}
}
}
回答by polygenelubricants
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
为此使用 a 。目前尚不清楚您的确切要求是什么,但这里有一个应该说明的示例:
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.
所以,这里所发生的是,Scanner
使用\s*
作为分隔符,这是“零个或多个空白字符”正则表达式。这会跳过输入中的空格等,因此您一次只能获得一个非空白字符。
回答by SolarBear
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.
通过使用 charAt 函数,您可以在不使用外部转换的情况下获取第一个字符的值。
回答by Marc We
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()
您还可以使用 String.charAt() 获得单个
回答by Ujjwal
.... char ch; ... ch=scan.next().charAt(0); . . It's the easy way to get character.
.... 字符 ch; ... ch=scan.next().charAt(0); . . 这是获得性格的简单方法。
回答by drcicero
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!
通常建议使用 nextline 和 System.in.read 要求用户在键入字符后按 Enter 键。但是,寻找此问题答案的人可能也有兴趣直接响应控制台中的按键!
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:
我找到了一个使用jline3这样做的解决方案,其中我们首先将终端更改为 rawmode 以直接响应按键,然后等待下一个输入的字符:
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>