简体   繁体   中英

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. Users will be copy and pasting any text such as an article would have multiple "\\n" character. 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.

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. 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. 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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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