Java 为什么 Scanner 类没有 nextChar 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18746185/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:41:00  来源:igfitidea点击:

Why doesn't the Scanner class have a nextChar method?

java

提问by Katana24

This is really a curiosity more than a problem...

这真的是一种好奇,而不是一个问题......

Why doesn't the Scannerclass have a nextChar()method? It seems like it should when you consider the fact that it has next, nextInt, nextLineetc method.

为什么Scanner类没有nextChar()方法?现在看来似乎应该当你认为它有一个事实nextnextIntnextLine等方法。

I realize you can simply do the following:

我意识到您可以简单地执行以下操作:

userChar = in.next().charAt(0);
System.out.println( userChar  );

But why not have a nextChar()method?

但是为什么没有nextChar()方法呢?

采纳答案by Frithjof

The reason is that the Scanner class is designed for reading in whitespace-separated tokens. It's a convenience class that wraps an underlying input stream. Before scanner all you could do was read in single bytes, and that's a big pain if you want to read words or lines. With Scanner you pass in System.in, and it does a number of read() operations to tokenize the input for you. Reading a single character is a more basic operation. Source

原因是 Scanner 类旨在读取以空格分隔的标记。这是一个包装底层输入流的便利类。在使用扫描仪之前,您只能以单个字节读取,如果您想读取单词或行,那将是一个很大的痛苦。使用 Scanner,您可以传入 System.in,它会执行许多 read() 操作来为您标记输入。读取单个字符是更基本的操作。 来源

You can use (char) System.in.read();.

您可以使用(char) System.in.read();.

回答by Marc Hauptmann

According to the javadoca Scannerdoes not seem to be intended for reading singlecharacters. You attach a Scannerto an InputStream(or something else) and it parses the input for you. It also can strip of unwanted characters. So you can read numbers, lines, etc. easily. When you need only the characters from your input, use a InputStreamReaderfor example.

根据javadocaScanner似乎不是用于读取单个字符。您将 a 附加Scanner到 an InputStream(或其他东西),它会为您解析输入。它还可以去除不需要的字符。因此,您可以轻松阅读数字、线条等。当您只需要输入中的字符时,例如使用InputStreamReader

回答by Damian Leszczyński - Vash

The Scanner class is bases on logic implemented in String next(Pattern)method. The additional API method like nextDouble()or nextFloat(). Provide the pattern inside.

Scanner 类基于在 Stringnext(Pattern)方法中实现的逻辑。附加的 API 方法,如nextDouble()nextFloat()。提供里面的图案。

Then class description says:

然后类描述说

A simple text scanner which can parse primitive types and strings using regular expressions.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

一个简单的文本扫描器,可以使用正则表达式解析原始类型和字符串。

Scanner 使用分隔符模式将其输入分解为标记,默认情况下与空格匹配。然后可以使用各种 next 方法将结果令牌转换为不同类型的值。

From the description it can be sad that someone has forgot about char as it is a primitive type for sure.

从描述来看,有人忘记了 char 可能很遗憾,因为它肯定是一种原始类型。

But the concept of class is to find patterns, a char has no pattern is just next character. And this logic IMHO caused that nextChar has not been implemented.

但是类的概念是寻找模式,没有模式的字符只是下一个字符。恕我直言,这个逻辑导致 nextChar 尚未实现。

If you need to read a filed char by char you can used more efficient class.

如果您需要按字符读取归档字符,您可以使用更高效的类。

回答by Vivin Paliath

I would imagine that it has to do with encoding. A charis 16 bytes and some encodings will use one byte for a character whereas another will use two or even more. When Java was originally designed, they assumed that any Unicode character would fit in 2 bytes, whereas now a Unicode character can require up to 4 bytes (UTF-32). There is no way for Scannerto represent a UTF-32 codepoint in a single char.

我想这与编码有关。Achar是 16 个字节,一些编码将使用一个字节作为字符,而另一种将使用两个甚至更多。最初设计 Java 时,他们假设任何 Unicode 字符都可以容纳 2 个字节,而现在一个 Unicode 字符最多需要 4 个字节 (UTF-32)。无法Scanner在单个char.

You can specify an encoding to Scannerwhen you construct an instance, and if not provided, it will use the platform character-set. But this still doesn't handle the issue with 3 or 4 byte Unicode characters, since they cannot be represented as a single charprimitive (since charis only 16 bytes). So you would end up getting inconsistent results.

您可以Scanner在构造实例时指定编码,如果未提供,它将使用平台字符集。但这仍然不能处理 3 或 4 字节 Unicode 字符的问题,因为它们不能表示为单个char原语(因为char只有 16 个字节)。所以你最终会得到不一致的结果。

回答by Stephen C

To get a definitive reason, you'd need to ask the designer(s) of that API.

要获得明确的原因,您需要询问该 API 的设计者。

But one possible reason is that the intent of a (hypothetical) nextCharwould not fit into the scanning model very well.

但一个可能的原因是(假设)的意图nextChar不太适合扫描模型。

  • If nextChar()to behaved like read()on a Readerand simply returned the next unconsumed character from the scanner, then it is behaving inconsistently with the other next<Type>methods. These skip over delimiter characters before they attempt to parse a value.

  • If nextChar()to behaved like (say) nextIntthen:

    • the delimiter skipping would be "unexpected" for some folks, and

    • there is the issue of whether it should accept a single "raw" character, or a sequence of digits that are the numeric representation of a char, or maybe even support escaping or something1.

  • 如果nextChar()to 表现得像read()aReader并简单地从扫描仪返回下一个未使用的字符,那么它的行为与其他next<Type>方法不一致。这些在尝试解析值之前跳过分隔符。

  • 如果nextChar()表现得像(说)nextInt那么:

    • 对于某些人来说,分隔符跳过将是“出乎意料的”,并且

    • 存在的问题是它是否应该接受单个“原始”字符,或作为 a 的数字表示的数字序列char,或者甚至可能支持转义或1

No matter what choice they made, some people wouldn't be happy. My guess is that the designers decided to stay away from the tarpit.

不管他们做出什么样的选择,总有一些人会不高兴。我的猜测是设计师决定远离油布。



1 - Would vote strongly for the raw character approach ... but the point is that there are alternatives that need to be analysed, etc.

1 - 会强烈投票支持原始角色方法......但关键是有需要分析的替代方案,等等。