簡體   English   中英

如何讓 Java 注冊一個帶空格的字符串輸入?

[英]How do I make Java register a string input with spaces?

這是我的代碼:

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

  if (question.equalsIgnoreCase("howdoyoulikeschool?") )
    /* it seems strings do not allow for spaces */
    System.out.println("CLOSED!!");
  else
    System.out.println("Que?");

當我試着寫“你喜歡學校嗎?” 答案總是“闕?” 但它作為“howdoyoulikeschool?”工作正常。

我應該將輸入定義為字符串以外的其他內容嗎?

in.next()將返回以空格分隔的字符串。 如果您想閱讀整行,請使用in.nextLine() 讀取字符串后,使用question = question.replaceAll("\\\\s","")刪除空格。

由於時間很長並且人們一直建議使用Scanner#nextLine() ,因此Scanner可能會在輸入中包含空格。

類掃描器

Scanner 使用分隔符模式將其輸入分解為標記,默認情況下與空格匹配。

您可以使用Scanner#useDelimiter()Scanner的分隔符更改為另一種模式,例如line feed或其他內容。

Scanner in = new Scanner(System.in);
in.useDelimiter("\n"); // use LF as the delimiter
String question;

System.out.println("Please input question:");
question = in.next();

// TODO do something with your input such as removing spaces...

if (question.equalsIgnoreCase("howdoyoulikeschool?") )
    /* it seems strings do not allow for spaces */
    System.out.println("CLOSED!!");
else
    System.out.println("Que?");

我今天在 Java 中發現了一個非常奇怪的東西,所以它就像 -

如果您從用戶輸入超過 1 項內容,請說

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
double d = sc.nextDouble();
String s = sc.nextLine();

System.out.println(i);
System.out.println(d);
System.out.println(s);

所以,看起來如果我們運行這個程序,它會要求這 3 個輸入並說我們的輸入值為 10, 2.5,“歡迎使用 java” 程序應該按原樣打印這 3 個值,因為我們已經使用了 nextLine () 所以它不應該忽略我們在變量s 中輸入的空格后的文本

但是,您將獲得的輸出是 -

10
2.5

就是這樣,它甚至不提示輸入字符串。 現在我正在閱讀它,老實說,我的理解仍然存在一些差距,我所能弄清楚的只是在我們按回車鍵輸入 int 和雙輸入之后,它認為這是提示並忽略了下一行()。

所以把我的代碼改成這樣 -

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
double d = sc.nextDouble();
sc.nextLine();
String s = sc.nextLine();

System.out.println(i);
System.out.println(d);
System.out.println(s);

完美地完成了這項工作,因此它與前面示例中存儲在鍵盤緩沖區中的“\\n”之類的東西有關,我們可以使用它繞過。

如果有人知道請幫我解釋一下。

代替

Scanner in = new Scanner(System.in);
String question;
question = in.next();

輸入

Scanner in = new Scanner(System.in);
String question;
question = in.nextLine();

這應該能夠將空格作為輸入。

這是在 java 中獲取輸入的示例實現,我僅在工資字段上添加了一些容錯以顯示它是如何完成的。 如果您注意到,您還必須關閉輸入流.. 享受 :-)

/* AUTHOR: MIKEQ
 * DATE: 04/29/2016
 * DESCRIPTION: Take input with Java using Scanner Class, Wow, stunningly fun. :-) 
 * Added example of error check on salary input.
 * TESTED: Eclipse Java EE IDE for Web Developers. Version: Mars.2 Release (4.5.2) 
 */

import java.util.Scanner;

public class userInputVersion1 {

    public static void main(String[] args) {

    System.out.println("** Taking in User input **");

    Scanner input = new Scanner(System.in);
    System.out.println("Please enter your name : ");
    String s = input.nextLine(); // getting a String value (full line)
    //String s = input.next(); // getting a String value (issues with spaces in line)

    System.out.println("Please enter your age : ");
    int i = input.nextInt(); // getting an integer

    // version with Fault Tolerance:
    System.out.println("Please enter your salary : ");
    while (!input.hasNextDouble())
    {
        System.out.println("Invalid input\n Type the double-type number:");
        input.next();
    }
    double d = input.nextDouble();    // need to check the data type?

    System.out.printf("\nName %s" +
            "\nAge: %d" +
            "\nSalary: %f\n", s, i, d);

    // close the scanner
    System.out.println("Closing Scanner...");
    input.close();
    System.out.println("Scanner Closed.");      
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM