简体   繁体   English

如何在Java中将类型为String的用户输入添加到ArrayList?

[英]How do I add a User Input of type String to an ArrayList in Java?

I am attempting to make a course registration system and one of my classes (Course) is centered around course attributes (ie. Course number, course name, instructors, students). 我正在尝试建立一个课程注册系统,并且我的一个课程(课程)围绕课程属性(即课程编号,课程名称,讲师,学生)进行。 I am making an ArrayList so that the Administrator (one of the user types) may add as many instructors to the course as he/she would like- I have created a Scanner and a String variable and everything, but when I write the .add command, Eclipse highlights ".add" and says "the method .add() is undefined for the type of scanner". 我正在制作一个ArrayList,以便管理员(用户类型之一)可以向他/她希望的课程中添加尽可能多的讲师-我创建了Scanner和String变量以及所有内容,但是当我编写.add时命令,Eclipse高亮显示“ .add”并说“对于扫描仪类型,未定义方法.add()”。 Now, I can understand this, but I have no idea how to fix it and I've tried so many ideas. 现在,我可以理解了,但是我不知道如何解决它,我已经尝试了很多想法。

Here is the method:` 这是方法:

public static String Instructor(){
        String courseInstructors;

        System.out.println("Please add name(s) of course instructors.");
        ArrayList<String> Instructors= new ArrayList<String>();
        Scanner courseInst = new Scanner(System.in);
        courseInstructors = courseInst.next();
        //courseInst.add(courseInstructors); 

        for(String courseInstructors1 : Instructors) {
            courseInstructors1 = courseInstructors;
            courseInst.add(courseInstructors1);
        }

        return;
    }`

Please adhere to Java naming conventions ad use lower case for variable names - instructors instead of Instructors . 请遵守Java命名约定,变量名称应使用小写字母- instructors而不是Instructors

Also, you want to add to your arraylist, so call add() on 此外,您要添加到您的ArrayList中,所以叫add()

instructors.add(courseInstructors1)

You may also want to consider choosing better variable naming than courseInstructors1 , for instance just courseInstructor , since you are referring to on instructor of all instructors . 您可能还需要考虑选择更好的变量命名比courseInstructors1 ,例如刚刚courseInstructor ,因为你是指在instructor所有的instructors

Also in your for loop you are doing the following 同样在for循环中,您正在执行以下操作

for(String courseInstructors1 : Instructors) {
    courseInstructors1 = courseInstructors;
    courseInst.add(courseInstructors1);
}

This can be simplified to 这可以简化为

for(String courseInstructors1 : Instructors) {
    courseInst.add(courseInstructors);
}

And if you look at the simplification you will see that iterating through Instructors make no sense here, since you are not using the contents of courseInstructors1. 而且,如果您看一下简化,您会发现遍历Instructor在这里没有意义,因为您没有使用courseInstructors1的内容。

I'm trying to understand what your loop is for. 我正在尝试了解您的循环的用途。

if you are trying to get multiple instructor names from one input then you need something like this. 如果您试图从一个输入中获取多个教师姓名,则需要这样的内容。

//get input
//"John Peggy Adam blah blah"
courseInstructors = courseInst.next();

//split the string by white space
String[] instArr = courseInstructors.split(" ");
//will give array of John, Peggy, Adam, blah, blah

Then do your foreach loop to add them to the list. 然后执行foreach循环将其添加到列表中。

for(String inst: instArr){
    instructors.add(inst);
}

Otherwise I would suggest doing something like this so you don't have to worry about splitting names and such. 否则,我建议您做这样的事情,这样您就不必担心拆分名称之类的事情。

courseInstructor = courseInst.nextLine();

while(!courseInstructor.equals("done"){
    //add name to list of instructors.
    instructors.add(courseInstructor);
    //get next name.
    courseInstructor = courseInt.nextLin();
    //if the user types done, it will break the loop.
    //otherwise come back around and add it and get next input.
}

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

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