如何使用扫描仪读取回车符作为字符串的一部分?

发布于 2024-11-25 06:13:33 字数 371 浏览 1 评论 0 原文

我正在编写一个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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜宝宝 2024-12-02 06:13:33

首先,注意:您应该查看 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().

他夏了夏天 2024-12-02 06:13:33

所以不要使用扫描仪。直接使用InputStream,将字节转换为字符串。

So do not use Scanner. Use InputStream directly and convert bytes to strings.

落墨 2024-12-02 06:13:33

无论你使用什么方法,你都必须有一些输入终止的迹象。在 Scanner 情况下,您可以useDelimiterScanner 的分隔符设置为您想要的任何内容。

您也可以查看此处此处

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 the Scanner to whatever you want.

Also you could take a look here and here.

预谋 2024-12-02 06:13:33

您知道那里有一个换行符,因为扫描程序已使用它来确定它检索到的行的末尾。您所要做的就是重新插入您所知道的内容:

while(userInput2.hasNextLine())
   toBeProcessed = toBeProcessed + userInput2.nextLine() + "\n";

Martin 关于使用 StringBuilder 的评论是正确的。这会将您的代码更改为如下所示:

while(userInput2.hasNextLine()) {
   toBeProcessedSB = toBeProcessedSB.append(userInput2.nextLine()); 
   toBeProcessedSB = toBeProcessedSB.append("\n"); 
}
toBeProcessed = toBeProcessedSB.toString();

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:

while(userInput2.hasNextLine())
   toBeProcessed = toBeProcessed + userInput2.nextLine() + "\n";

Martin's comment about using StringBuilder is correct. That would change your code to look like:

while(userInput2.hasNextLine()) {
   toBeProcessedSB = toBeProcessedSB.append(userInput2.nextLine()); 
   toBeProcessedSB = toBeProcessedSB.append("\n"); 
}
toBeProcessed = toBeProcessedSB.toString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文