2010-10-30 161 views
11

我正在编写一个程序,允许用户输入他的数据然后输出它。它的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); 
    } 
} 

回答

5

扫描仪的默认分隔符是空格。请检查javadoc以了解如何更改此设置。

+0

更新网址为JavaSE中7 :) – saikosen 2013-10-11 06:21:16

4

而不是直接使用System.inSystem.out的,使用Console类 - 它可以让你显示一个提示和读取整条生产线的一个呼叫输入的(由此固定您的问题):

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

任何其他简单的办法,因为我还没有但了解了控制台类? – Bonett09 2010-10-30 13:49:28

+1

@ Bonett09:恩,现在你已经了解了它。它实际上在某种程度上比直接使用输入和输出流更简单。 – 2010-10-30 14:03:22

9

我会使用Scanner#nextLine反对next为您的address属性。

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

由于这将返回整行,由行分隔符分隔,它将允许用户输入任何地址而没有任何约束。

-4

你可以这样做:

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); 

    } 
} 
+1

这个答案是不正确的。如果你用“s.nextLine();”来测试这段代码,将不返回任何内容 - 一个空字符串。不要使用这个答案! – Rubinum 2014-11-05 08:33:30

+0

@Rubinum这里的代码没有错(逻辑上错误,是的,但不是技术上),如果你的代码返回一个空字符串,那么你没有正确使用's.nextLine()'。这不是user114573s的错。 – Tom 2017-10-17 03:32:09

7

初始化扫描仪这样使得它使用新的行字符界定输入。

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

请参考JavaDoc更多细节

使用sc.next()来获得整条生产线的字符串

+0

但是\ r仍然在String中。 – 2016-10-24 19:58:33

+0

能阅读完整篇文章,内容包括空格和特殊字符。使用sc.next()。trim()修剪用户输入中的[Enter]。 – oraclesoon 2017-03-16 09:21:09

+0

它应该是Scanner sc = new Scanner(System.in).useDelimiter(“\ n”); – 2017-10-28 16:38:42

0

我有同样的问题。这应该为你工作:

s.nextLine(); 
15

这种方法的工作,但我并不怎么样,任何人都可以解释,它是如何工作的..

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

因为方法'nextLine()'在光标当前位置之后从当前当前行读取下一个符号。例如,如果你的输入包含2行'1'和'Hello World'。和这样的源代码:'int x = sc.nextInt(); String s = sc.nextLine()'。在这种情况下,在'nextInt()'后面,光标停留在符号“1”后面,这是该行的结尾,nextLine()不会读取任何内容。但是,当你写代码时,首先写光标,光标移动到下一行并读取单词“你好”。然后光标停留在单词“你好”之后。命令'sc.nextLine()'读取下一个符号 - “世界”。 – Ulphat 2016-08-12 06:03:23

+0

有史以来第一次看到一个答案,这本身就是一个问题得到了许多upvotes – Ravi 2017-10-18 14:57:14

-3

使用nextLine(),而不是未来的( )作为字符串输入的句子。

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

要得到整个ADRESS添加

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

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

你应该试试这个代码:

scan.nextLine(); 
String address = s.nextLine(); 

这完全适用。

4
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*/ 
0

我设置令牌的分隔符是一个换行模式,读取下一个标记,然后把分隔符回不管它是什么。

public static String readLine(Scanner scanner) { 
     Pattern oldDelimiter = scanner.delimiter(); 
     scanner.useDelimiter("\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029]"); 
     String r = scanner.next(); 
     scanner.useDelimiter(oldDelimiter); 
     return r; 
} 
2
import java.util.Scanner; 
public class Solution { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     int i = scan.nextInt(); 
     double d = scan.nextDouble(); 
     String s=""; 
     while(scan.hasNext()) 
     { 
      s=scan.nextLine(); 
     } 

     System.out.println("String: " +s); 
     System.out.println("Double: " + d); 
     System.out.println("Int: " + i); 
    } 
} 
+2

添加一些解释和答案这个答案如何帮助OP在修复当前问题 – 2017-07-17 15:13:04

0

下一个()可以读取输入只能做到的空间。它不能读取由空格分隔的两个单词。另外,next()在读取输入后将光标放在同一行中。

nextLine()读取包含单词之间空格的输入(也就是说,它读取直到行\ n结束)。一旦输入被读取,nextLine()将光标定位在下一行。

读取整条生产线就可以使用nextLine()

0

我尝试下面的代码,但是这并不阻止,因为没有休息状态,因此始终等待输入,可能是你能为破添加条件

public class Solution { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     int i = scan.nextInt(); 
     double d = scan.nextDouble(); 
     String s=""; 
     while(scan.hasNext()) 
     { 
      s=scan.nextLine(); 
     } 

     System.out.println("String: " +s); 
     System.out.println("Double: " + d); 
     System.out.println("Int: " + i); 
    } 
} 
+1

这并不回答这个问题。将您的查询发布为新问题。 – 2017-08-26 03:25:21

0

以下是使用Java扫描仪类在标准输入中读取一行的示例代码。

import java.util.Scanner; 

public class Solution { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     String s = scan.nextLine(); 
     System.out.println(s); 
     scan.close(); 
    } 
} 

输入:

的Hello World

输出:

的Hello World

0

“只打印一句话让从'大主教街'只说'大主教'

它只是打印这个词,因为扫描仪的功能如next(),nextInt()等。因此这个函数读取并返回下一个标记。

For example if you have: x y z 
s.next() will return x 
s.nextLine() y z 

再回到你的代码,如果你想阅读整行“大主教街”

String address = s.next(); // s = "Archbishop" 
Then address += s.nextLine(); // s = s + " Street" 
0

我们可以通过scan.nextLine。它会读取输入,直到其余轻松完成结束。然后将其分配给您的变量。整个句子可以轻松打印 。以下是您更好理解的例子。

String s = "HackerRank "; 

    Scanner scan = new Scanner(System.in); 


    String s2; 



    scan.nextLine(); // read the rest of the line of input (newline character after the double token). 
    s2 = scan.nextLine(); 



    /* Concatenate and print the String variables on a new line integer variables on a new line; 

    System.out.println(s + s2); 

    scan.close(); 

}}

0

下面的代码应该做的事情yoiu正在寻找:

public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    String s = scan.nextLine(); 
    System.out.println(s); 
    scan.close(); 
}