Scanner.next()和Scanner.nextLine() [英] Scanner.next() and Scanner.nextLine()

查看:89
本文介绍了Scanner.next()和Scanner.nextLine()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

Scanner in = new Scanner (System.in);
String[] data = new String[5];

System.out.println("Please, enter the name of the customer ordering:");
data[0] = in.next();
System.out.print("Please, enter the assembly details: ");
data[1] = in.nextLine();
System.out.print("Please, enter the assembly id:");
data[2] = in.next();
System.out.println("Please, enter the date the assembly was ordered (MM-DD-YYYY):");
data[3] = in.next();

我试图让nextLine()读取多个单词,但是现在测试它,它只是跳转到下一个扫描数据[2]。我需要帮助。我不知道该怎么做。

I am trying to get nextLine() to read more than a single word, but at the moment of testing it, it simply jumps to the next Scanning for data[2]. I need help. I have no idea what to do.

推荐答案

这应该有效:

    Scanner in = new Scanner (System.in);
    String[] data = new String[5];

    System.out.println("Please, enter the name of the customer ordering:");
    data[0] = in.nextLine();
    System.out.println("Please, enter the assembly details: ");
    data[1] = in.nextLine();
    System.out.println("Please, enter the assembly id:");
    data[2] = in.nextLine();
    System.out.println("Please, enter the date the assembly was ordered (MM-DD-YYYY):");
    data[3] = in.nextLine();
    in.close();

你应该使用 nextLine()和不是 next()从控制台读取每一行

You should be using nextLine() and not next() to read each line from console

这篇关于Scanner.next()和Scanner.nextLine()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆