简体   繁体   English

我无法弄清楚如何将用户输入放在逗号分隔的列表中并将其放入ArrayList中

[英]I cannot figure out how to take user input in a comma-separated list and place it into an ArrayList

I'm trying to present the user with text, then the user types a comma-separated list of full names (first and last) then hits enter, each full name is placed into an ArrayList, then the elements of the list are displayed back to the user. 我正在尝试向用户显示文本,然后用户输入逗号分隔的全名列表(第一个和最后一个),然后按回车键,将每个全名放入ArrayList中,然后将列表的元素显示回去给用户。

So I tried this: 所以我尝试了这个:

Scanner in = new Scanner(System.in);
ArrayList<String> members = new ArrayList<String>();
String[] groupName = new String[1];
System.out.print("Group Name: ");
groupName[0] = in.next().toLowerCase();
System.out.print("Names: ");
in.useDelimiter(",| ");
while (in.hasNext()){
    members.add(in.next().toLowerCase();
}
for (String m : members)
{
  System.out.println(m + ", ");
}

What happens is "Group Name: " displays, I type whatever I want the name to be then hit enter, then "Names: " displays, I enter "a aa, b bb, c cc" and hit enter, then nothing happens. 发生的是显示“组名:”,我键入要输入的名称,然后按Enter,然后显示“名称:”,我输入“ aa,b bb,c cc”并按Enter,然后什么也没有发生。 What I expected to happen is the first element of the arraylist is set to "a aa", the second element to "b bb", and the third to "c cc" so it would print "a aa, b bb, c cc, " (I know this looks bad but it doesn't really matter, I'm just using this for now to make sure the ArrayList is being populated properly, which it's not). 我期望发生的事情是将arraylist的第一个元素设置为“ aa a”,将第二个元素设置为“ b bb”,第三个元素设置为“ c cc”,因此它将打印“ aa,b bb,c cc” ,“(我知道这看起来很糟,但这并不重要,我现在仅使用它来确保ArrayList正确填充了,不是)。 But the scanner seems to think there's still input so it gets stuck in the while loop. 但是扫描器似乎认为仍然有输入,因此卡在了while循环中。 I've been trying a lot of different ways to fix this, some very convoluted using booleans in an attempt to break out of the while loop after hitting enter. 我一直在尝试许多不同的方法来解决此问题,其中一些使用布尔值非常令人费解,以尝试在按下回车键后退出while循环。 I've been trying for hours and everything has failed. 我已经尝试了几个小时,但一切都失败了。 I'd really appreciate any help. 我真的很感谢您的帮助。

Your problem is that there is no way to stop your execution. 您的问题是无法停止执行。

Could you just read the whole line of names using names = in.nextLine() and use String.split(",") to split the values? 您可以使用names = in.nextLine()读取整行名称,并使用String.split(",")拆分值吗?

Scanner in = new Scanner(System.in);
String[] groupName = new String[1];
System.out.print("Group Name: ");
groupName[0] = in.nextLine();
System.out.print("Names: ");
String names = in.nextLine();
String[] members = names.split(",");
System.out.println(Arrays.toString(members));
in.close();

There are two problems. 有两个问题。 First, use in.nextLine() instead of in.next().toLowerCase(). 首先,使用in.nextLine()代替in.next()。toLowerCase()。 Then use split() function of taken input string and then divide the string in the array. 然后使用获取的输入字符串的split()函数,然后在数组中分割该字符串。 Please try the following code. 请尝试以下代码。

    Scanner in = new Scanner(System.in);
    ArrayList<String> members = new ArrayList<String>();
    System.out.print("Group Name: ");
    String ab = in.nextLine();
    String[] splitString = ab.split(", ");
    for (String string : splitString)
    {
        //System.out.println("splited string: " + string);
        members.add(string);
    }
    System.out.println("Names: ");
    for (String m : members)
    { 
        System.out.println(m + ", ");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM