12

这是我的代码:

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

当我试着写“你喜欢学校吗?” 答案总是“阙?” 但它可以很好地作为“你喜欢学校吗?”

我应该将输入定义为字符串以外的内容吗?

4

5 回答 5

18

in.next()将返回以空格分隔的字符串。in.nextLine()如果您想阅读整行,请使用。读取字符串后,使用question = question.replaceAll("\\s","")删除空格。

于 2012-09-21T04:32:09.197 回答
4

由于它很长一段时间并且人们一直建议使用Scanner#nextLine(),因此还有另一个机会Scanner可以占用输入中包含的空格。

类扫描仪

Scanner 使用分隔符模式将其输入分解为标记,默认情况下匹配空格。

您可以使用Scanner#useDelimiter()将分隔符更改Scanner为另一种模式,例如 aline 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?");
于 2018-01-04T06:51:53.817 回答
3

我今天在 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、“Welcome to java” 程序应该按原样打印这三个值,因为我们使用了 nextLine () 所以它不应该忽略我们在变量s中输入的空格之后的文本

但是,您将得到的输出是 -

10
2.5

就是这样,它甚至不提示输入字符串。现在我正在阅读它,老实说,我的理解仍然存在一些差距,我所能弄清楚的是在输入 int 输入然后当我们按 enter 时的双重输入后,它认为这是提示并忽略了下一行()。

所以把我的代码改成这样 -

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”之类的内容有关,我们可以使用它绕过。

如果有人知道,请帮我解释一下。

于 2020-11-24T16:59:24.750 回答
2

Instead of

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

Type in

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

This should be able to take spaces as input.

于 2015-04-10T22:49:28.057 回答
1

这是在 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.");      
}
}
于 2016-05-02T15:52:28.943 回答