2010-10-30 160 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(); 
}