我正在编写一个java程序,需要一个长字符串作为用户的输入。这些字符串大部分是从互联网上复制和粘贴的新闻文章。我遇到的问题是,当用户输入文本时,它包含扫描仪无法处理的回车符。我正在使用 Scanner 类中的 nextLine() 方法,问题是回车符标记行尾,从而终止从 Scanner 的读取。我也尝试过以下代码,但这会导致无限循环。 UserInput2 是一个扫描器。
while(userInput2.hasNextLine())
toBeProcessed = toBeProcessed + userInput2.nextLine();
有人知道如何跳过回车符吗?也许让扫描仪将它们转换为“+”或类似的东西,这样我就不会遇到这个问题。
I'm writing a java program that will require a long String as input from the user. Most of these Strings are news articles copied and pasted off the internet. The problem I've having is that when the user enters the text, it contains carriage returns that Scanner has trouble processing. I'm using the nextLine() method from the Scanner class and the problem is that the carriage returns mark the end of lines and thus terminate that read from the Scanner. I've tried the following code, too, but that results in an infinite loop. UserInput2 is a Scanner.
while(userInput2.hasNextLine())
toBeProcessed = toBeProcessed + userInput2.nextLine();
Anyone know how I can skip over the carriage returns? Maybe have the scanner convert them to a '+' or something like that so I don't have this problem.
发布评论
评论(4)
首先,注意:您应该查看 StringBuilder 类。
其次,您可以将 useDelimiter 用于永远不会出现在输入字符串中的内容,或者更好的方法是将 Apache commons IOUtils 与 InputStream 一起使用,如 这个问题而不是使用扫描仪,这为您提供了指定字符编码的好处。
您可以使用 String.replace()。
First, a note: Instead of concatenating String objects you should look into the StringBuilder class.
Second, you could use useDelimiter to something that will never be in the input string, or a nicer method would be to use the Apache commons IOUtils with the InputStream as in this question rather than using a Scanner, which gives you the benefit of specifying a character encoding.
You can strip '\r' characters with String.replace().
所以不要使用扫描仪。直接使用InputStream,将字节转换为字符串。
So do not use Scanner. Use InputStream directly and convert bytes to strings.
无论你使用什么方法,你都必须有一些输入终止的迹象。在
Scanner
情况下,您可以useDelimiter
将Scanner
的分隔符设置为您想要的任何内容。您也可以查看此处和此处。
No matter what method you are using, you must have some sign of input termination. In the
Scanner
case, you could uuseDelimiter
to set the delimiter of theScanner
to whatever you want.Also you could take a look here and here.
您知道那里有一个换行符,因为扫描程序已使用它来确定它检索到的行的末尾。您所要做的就是重新插入您所知道的内容:
Martin 关于使用 StringBuilder 的评论是正确的。这会将您的代码更改为如下所示:
You know that there was a newline there because Scanner has used it to determine the end of the line it retrieved. All you have to do is to reinsert what you know was there:
Martin's comment about using
StringBuilder
is correct. That would change your code to look like: