在Java中将多行读入扫描仪对象

时间:2022-03-15 23:15:53

I'm having a bit of trouble figuring out how to read multiple lines of user input into a scanner and then storing it into a single string. What I have so far is down below:

我在弄清楚如何将多行用户输入读入扫描仪然后将其存储到单个字符串中时遇到了一些麻烦。我到目前为止的内容如下:

public static String getUserString(Scanner keyboard) { 
    System.out.println("Enter Initial Text:");
    String input = "";
    String nextLine = keyboard.nextLine();
    while(keyboard.hasNextLine()){
        input += keyboard.nextLine
    };
    return input;
}

then the first three statements of the main method is:

那么主要方法的前三个陈述是:

Scanner scnr = new Scanner(System.in);
String userString = getUserString(scnr);  
System.out.println("\nCurrent Text: " + userString );

My goal is to have it where once the user types their text, all they have to do is hit Enter twice for everything they've typed to be displayed back at them (following "Current text: "). Also I need to store the string in the variable userString in the main (I have to use this variable in other methods). Any help at all with this would be very much appreciated. It's for class, and we can't use arrays or Stringbuilder or anything much more complicated than a while loop and basic string methods.

我的目标是让用户输入文本后,只需按两次输入键,输入两次以显示回来的内容(按照“当前文字:”)。另外我需要将字符串存储在main中的变量userString中(我必须在其他方法中使用此变量)。任何有关这方面的帮助将非常感谢。它是用于类的,我们不能使用数组或Stringbuilder或任何比while循环和基本字符串方法复杂得多的东西。

Thanks!

1 个解决方案

#1


2  

Using BufferedReader:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = "";
String line;
while((line = br.readLine()) != null){
    if(line.isEmpty()){
        break; // if an input is empty, break
    }
    input += line + "\n";
}
br.close();
System.out.println(input);

Or using Scanner:

或使用扫描仪:

String input = "";
Scanner keyboard = new Scanner(System.in);
String line;
while (keyboard.hasNextLine()) {
    line = keyboard.nextLine();
    if (line.isEmpty()) {
        break;
    }
    input += line + "\n";
}
System.out.println(input);

For both cases, Sample I/O:

对于这两种情况,样本I / O:

Welcome to *
Hello My friend
Its over now

Welcome to *
Hello My friend
Its over now

Complete code

public static void main (String[] args) {
    Scanner scnr = new Scanner(System.in);
    String userString = getUserString(scnr);  
    System.out.println("\nCurrent Text: " + userString);
}

public static String getUserString(Scanner keyboard) { 
    System.out.println("Enter Initial Text: ");
    String input = "";
    String line;
    while (keyboard.hasNextLine()) {
        line = keyboard.nextLine();
        if (line.isEmpty()) {
            break;
        }
        input += line + "\n";
    }
    return input;
}

#1


2  

Using BufferedReader:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = "";
String line;
while((line = br.readLine()) != null){
    if(line.isEmpty()){
        break; // if an input is empty, break
    }
    input += line + "\n";
}
br.close();
System.out.println(input);

Or using Scanner:

或使用扫描仪:

String input = "";
Scanner keyboard = new Scanner(System.in);
String line;
while (keyboard.hasNextLine()) {
    line = keyboard.nextLine();
    if (line.isEmpty()) {
        break;
    }
    input += line + "\n";
}
System.out.println(input);

For both cases, Sample I/O:

对于这两种情况,样本I / O:

Welcome to *
Hello My friend
Its over now

Welcome to *
Hello My friend
Its over now

Complete code

public static void main (String[] args) {
    Scanner scnr = new Scanner(System.in);
    String userString = getUserString(scnr);  
    System.out.println("\nCurrent Text: " + userString);
}

public static String getUserString(Scanner keyboard) { 
    System.out.println("Enter Initial Text: ");
    String input = "";
    String line;
    while (keyboard.hasNextLine()) {
        line = keyboard.nextLine();
        if (line.isEmpty()) {
            break;
        }
        input += line + "\n";
    }
    return input;
}