问题描述
我需要从提示中获取一个字符串,然后在我需要从提示中获取一个字符串列表之后.我有个问题.当我拿到我的清单时,我只有第一个词.我尝试任何其他 nextLine()
......但不起作用.我看到很多样本只有 2 个单词(一个空格),但我正在寻找一个单词列表!!
I need get a String from prompt, and juste after I need get a String list from prompt. I have a problem. When I get my list, I have only the first word. I try any other nextLine()
... but do not work. I see lot of sample with juste 2 words (one space) but i looking for a list of word!!
Java 代码:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a name:");
String name = input.next();
System.out.println("Enter a name list:");
String nameList = input.next();
System.out.println("Enter last name:");
String lastName = input.next();
input.close();
System.out.println(name + " * " + nameList + " ** " + lastName);
}
控制台结果:
Enter a name:
aa
Enter a name list:
bb cc dd
Enter last name:
aa * bb ** cc
第一个响应是 aa + enter
1st response is aa + enter
第二个响应是 bb cc dd + enter
2nd response is bb cc dd + enter
但是在dd + enter之后,程序显示输入姓氏:aa * bb ** cc
并停止
but juste after dd + enter, the program display Enter last name:aa * bb ** cc
and stop
推荐答案
solution .nextLine()
但在所有行
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a name:");
String name = input.nextLine();
System.out.println("Enter a name list:");
String nameList = input.nextLine();
System.out.println("Enter last name:");
String lastName = input.nextLine();
input.close();
System.out.println(name + " * " + nameList + " ** " + lastName);
}
控制台:
Enter a name:
aa
Enter a name list:
bb cc dd
Enter last name:
ee
aa * bb cc dd ** ee
这篇关于java prompt + get a string 包含任何空格(一个或多个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!