Java 扫描仪不读取整个句子 - 扫描仪类的 next() 和 nextLine() 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4058912/
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-14 11:05:34  来源:igfitidea点击:

Scanner doesn't read whole sentence - difference between next() and nextLine() of scanner class

javajava.util.scanner

提问by Bonett09

I'm writing a program which allows the user to input his data then outputs it. Its 3/4 correct but when it arrives at outputting the address it only prints a word lets say only 'Archbishop' from 'Archbishop Street'. How do I fix this?

我正在编写一个程序,允许用户输入他的数据然后输出它。它的 3/4 正确,但是当它到达输出地址时,它只打印一个单词,让我们只说“大主教街”中的“大主教”。我该如何解决?

import java.util.*;

class MyStudentDetails{
    public static void main (String args[]){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter Your Name: ");
        String name = s.next();
        System.out.println("Enter Your Age: ");
        int age = s.nextInt();
        System.out.println("Enter Your E-mail: ");
        String email = s.next();
        System.out.println("Enter Your Address: ");
        String address = s.next();

        System.out.println("Name: "+name);
        System.out.println("Age: "+age);
        System.out.println("E-mail: "+email);
        System.out.println("Address: "+address);
    }
}

回答by Raghuram

Default delimiter of Scanner is whitespace. Check javadocfor how to change this.

Scanner 的默认分隔符是空格。检查javadoc以了解如何更改它。

回答by Michael Borgwardt

Instead of using System.inand System.outdirectly, use the Consoleclass - it allows you to display a prompt and read an entire line (thereby fixing your problem) of input in one call:

不是直接使用System.inand System.out,而是使用Console该类 - 它允许您在一次调用中显示提示并读取整行输入(从而解决您的问题):

String address = System.console().readLine("Enter your Address: ");

回答by Anthony Forloney

I would use Scanner#nextLineopposed to nextfor your addressattribute.

我会用Scanner#nextLine反对next你的address属性。

This method returns the rest of the current line, excluding any line separator at the end.

此方法返回当前行的其余部分,不包括末尾的任何行分隔符。

Since this returns the entire line, delimited by a line separator, it will allow the user to enter any address without any constraint.

由于这将返回由行分隔符分隔的整行,因此它将允许用户无限制地输入任何地址。

回答by user114573

You can do this:

你可以这样做:

class MyStudentDetails{
    public static void main (String args[]){
         Scanner s = new Scanner(System.in);
         System.out.println("Enter Your Name: ");
         String name = s.nextLine();
         System.out.println("Enter Your Age: ");
         String age = s.nextLine();
         System.out.println("Enter Your E-mail: ");
         String email = s.nextLine();
         System.out.println("Enter Your Address: ");
         String address = s.nextLine();


         System.out.println("Name: "+name);
         System.out.println("Age: "+age);
         System.out.println("E-mail: "+email);
         System.out.println("Address: "+address);

    }
}

回答by Nomad

Initialize the Scanner this way so that it delimits input using a new line character.

以这种方式初始化扫描器,以便它使用换行符分隔输入。

Scanner sc = new Scanner(System.in).useDelimiter("\n");

Refer the JavaDocfor more details

有关更多详细信息,请参阅JavaDoc

Use sc.next() to get the whole line in a String

使用 sc.next() 获取字符串中的整行

回答by Eugene Prokopenko

I had the same question. This should work for you:

我有同样的问题。这应该适合你:

s.nextLine();

回答by Arvind Ramachandran

This approach is working, but I don't how, can anyone explain, how does it works..

这种方法有效,但我不知道它是如何工作的,谁能解释一下。

String s = sc.next();
s += sc.nextLine();

回答by Akshay Suresh

Use nextLine()instead of next()for taking sentence as string input.

使用nextLine(),而不是next()采取一句字符串输入。

Scanner sc = new Scanner(System.in);
String s = sc.nextLine();

回答by Bharathi Mohan

To get the whole adress add

要获取整个地址添加

s.nextLine();//read the rest of the line as input

before

System.out.println("Enter Your Address: ");
String address = s.next();

回答by Vinayak

String s="Hi";
String s1="";

//For Reading Line by hasNext() of scanner 
 while(scan.hasNext()){
  s1 = scan.nextLine();
 }
System.out.println(s+s1);

/*This Worked Fine for me for reading Entire Line using Scanner*/