简体   繁体   English

使用 Scanner 读取用户输入时是否可以使用 enter 键作为分隔符,但不能使用“\\n”?

[英]Can I use the enter key as a delimiter when using Scanner to read user input, but not "\n"?

I am trying to use Scanner to read user input and use the "enter key" as a delimiter.我正在尝试使用 Scanner 读取用户输入并使用“输入键”作为分隔符。 Users will be copy and pasting any text such as an article would have multiple "\\n" character.用户将复制和粘贴任何文本,例如一篇文章将有多个“\\n”字符。 The issue is that only the first paragraph of any article is able to be read up until the first paragraph break.问题是,在第一段中断之前,只能阅读任何文章的第一段。 I want to read anything up until the user presses the "enter key".我想阅读任何内容,直到用户按下“输入键”。 I'm hoping there's a way to either single out the "enter key", or maybe change the value Scanner interprets when I press the enter key to something unique.我希望有一种方法可以选择“输入键”,或者当我按下输入键时将扫描仪解释的值更改为唯一的值。

I'm not sure what to try.我不知道该尝试什么。 It has to be the enter key that ends reading the input and not some other key, but I can't find any way to differentiate the "Enter Key" from a line break in the input.它必须是结束读取输入的输入键,而不是其他一些键,但我找不到任何方法来区分“输入键”和输入中的换行符。

This is what I have so far:这是我到目前为止:

public static String getInput()
    {
        Scanner reader = new Scanner(System.in);
        reader.useDelimiter("\n");
        String input = "";
        boolean finished = false;
        try {
            input += reader.next();
        }
        catch (Exception e)
        {
            finished = true;
            System.out.println(e);
        }
        return input;
    }

No, that is not something you can easily do with a Scanner , or even with System.in in general.不,这不是您可以使用Scanner轻松完成的事情,甚至通常使用System.in也无法做到。

System.in only sees the input to your program when it has already assembled into a stream of characters by whatever software you're using around the Java program to provide a window you can type things into and print to. System.in仅在程序已通过您在 Java 程序周围使用的任何软件组装成字符流时才能看到程序的输入,以提供一个窗口,您可以在其中输入内容并打印到其中。 By that time it is very likely that presses of the Enter key is already represented as a \\n character, same as the \\n that represents line breaks in pasted text.到那时,很可能按下 Enter 键已经表示为\\n字符,与表示粘贴文本中换行符的\\n相同。 This is not under the control of your Java program -- it is at the mercy of the terminal/console software that it is communicating with.这不受 Java 程序的控制——它受与之通信的终端/控制台软件的支配。

The only halfway reliable way to distinguish between keypresses and pasted text is to explicitly use a windowing toolkit and have your Java program display its own window that interacts with the OS-provided keyboard and clipboard functionalities on directly.区分按键和粘贴文本的唯一可靠方法是明确使用窗口工具包,并让您的 Java 程序显示自己的窗口该窗口直接与操作系统提供的键盘和剪贴板功能进行交互。

Doing that well is quite a bit a way up the learning curve, and if you're at the stage where new Scanner(System.in) is your go-to way of processing input, it will probably be more productive for you all in all to change your UI ambitions to something that is easier to do with those tools.做得好是学习曲线上的一大步,如果您处于new Scanner(System.in)是您处理输入的new Scanner(System.in)方式的阶段,它可能对您所有人来说都更有效率所有这些都是为了将​​您的 UI 野心改变为更容易使用这些工具完成的事情。

Here you go, I hope it helps.给你,希望对你有帮助。

import java.util.*;
public class MyClass {
   public static void main(String args[]) {
      StringBuilder sb = new StringBuilder();
      Scanner sc = new Scanner(System.in);
      while(sc.hasNext()){
         String line = sc.nextLine().trim();
         if(!line.equalsIgnoreCase("end")){ //delimiter check
            sb.append(line);
            sb.append("\n");
         }else
            break;
     }
     System.out.println(sb.toString());
   }
}

Input:输入:

Hello World!


This is para1.

This is para2.

Output:输出:

Hello World!


This is para1.

This is para2.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM