Java 使用单个扫描仪对象扫描多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22929819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:07:18  来源:igfitidea点击:

Scanning multiple lines using single scanner object

javainputoutputjava.util.scanner

提问by Creative_Cimmons

I a newbie to java so please don't rate down if this sounds absolute dumb to you

我是java的新手,所以如果这对你来说听起来绝对愚蠢,请不要评价

ok how do I enter this using a single scanner object

好的,我如何使用单个扫描仪对象输入此内容

5

hello how do you do

welcome to my world

6 7

5

你好,你怎么样

欢迎来到我的世界

6 7

for those of you who suggest

对于那些建议的人

scannerobj.nextInt->nextLine->nextLine->nextInt->nextInt,,,

check it out, it does not work!!!

看看,没用!!!

thanks

谢谢

采纳答案by ifloop

public static void main(String[] args) {
    Scanner  in    = new Scanner(System.in);

    System.out.printf("Please specify how many lines you want to enter: ");        
    String[] input = new String[in.nextInt()];
    in.nextLine(); //consuming the <enter> from input above

    for (int i = 0; i < input.length; i++) {
        input[i] = in.nextLine();
    }

    System.out.printf("\nYour input:\n");
    for (String s : input) {
        System.out.println(s);
    }
}

Sample execution:

示例执行:

Please specify how many lines you want to enter: 3
Line1
Line2
Line3

Your input:
Line1
Line2
Line3

回答by Jay kumar

try this code

试试这个代码

Scanner  in    = new Scanner(System.in);

System.out.printf("xxxxxxxxxxxxxxx ");        
String[] input = new String[in.nextInt()];

for (int i = 0; i < input.length; i++) {
    input[i] = in.nextLine();
}

for (String s : input) {
    System.out.println(s);
}

回答by Raghuveer Reddy

public class Sol{

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in); 

       while(sc.hasNextLine()){

           System.out.println(sc.nextLine());
       }

    }
}

回答by Jouberto Fonseca

You can try only with lambda too:

您也可以仅使用 lambda 尝试:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    scanner.forEachRemaining(input -> System.out.println(input));
}

回答by Sudarshan

may be we can use this approach:

也许我们可以使用这种方法:

for(int i = 0; i < n; i++)
   {
       Scanner sc1 = new Scanner(System.in);
       str0[i] = sc1.nextLine();
       System.out.println(str0[i]);
   }

that is we create the scanner object every time before we read the nextLine. :)

也就是说,我们每次在读取 nextLine 之前都会创建扫描仪对象。:)

回答by chirr

By default, the scanner uses space as a delimiter. In order to scan by lines using forEachRemaining, change the scanner delimiter to line as below.

默认情况下,扫描仪使用空格作为分隔符。要使用 forEachRemaining 按行扫描,请将扫描仪分隔符更改为 line,如下所示。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    scanner.useDelimiter("\n");
    scanner.forEachRemaining(System.out::println);
}