【问题标题】:Adding parameters to arraylist with Java使用Java将参数添加到arraylist
【发布时间】:2013-10-01 03:16:22
【问题描述】:

我正在测试我的大学班级,当用户输入“添加”时,它会在大学班级的数组列表中添加一个新学生。

   Scanner myInput=new Scanner (System.in);
   String information=myInput.next(); //I know this is incorrect not sure what else to do
    directory.addStudent(information);

这里的目录是包含学生的数组列表的对象。 addStudent 方法在 College 类中,如下所示:

  public void addStudent (String studentName, long studentID, String address) {
        Student newStu= new Student( studentname, studentID, address);
        collegeList.add(newStu); //here collegeList is the name of the arraylist in College class
   }

无论如何,我的问题似乎很简单,但无法弄清楚。我想知道如何拆分信息,以便它可以满足 addStudent 方法中的参数...如您所见,信息将是一个字符串,但我希望 studentID 是一个长字符串而不是字符串我该怎么办?

【问题讨论】:

  • 查看各种X.parseX() 方法,其中X 是Number 的常见子类型。
  • 提供有关您的输入的一些信息。输入中如何区分姓名、身份证和地址?通过换行符?空格?逗号?

标签: java arraylist java.util.scanner


【解决方案1】:

读取后不需要拆分字符串。您可以使用 Scanner 读取分隔字符串。

我假设您的输入由空格分隔。如果是这样,您需要这样做:

    String name = myInput.next();
    long id = myInput.nextLong();
    String address = myInput.next();
    dictionary.add(name, id, address);

【讨论】:

    【解决方案2】:

    试试这个:

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    public class StudentMain {
        private static List<Student> collegeList = new ArrayList<Student> (); 
        public static void main(String...args)throws Throwable {
             String s= null; String name=null; long id=0L; String address = null; 
            System.out.print("What do you want to do today, press q to quit : ");
            Scanner sc = new Scanner(System.in);
            do{
            try{    
                    s = sc.next();
                    if(s.equalsIgnoreCase("add")){
                        System.out.print("Enter the name of student:  ");
                        name = sc.next();
                        System.out.print("Enter the Id of student : " );
                        id=sc.nextLong();
                        System.out.print("Enter address of student:  ");
                        address = sc.next();
                        addStudent(name,id,address);
                    }
            }catch(NumberFormatException e){
                if(!s.equalsIgnoreCase("q"))
                    System.out.println("Please enter q to quit else try again ==> ");
                }
            }while(!s.equalsIgnoreCase("q"));
        sc.close();
        }
        private static void addStudent(String name, long id, String address) {
            // TODO Auto-generated method stub
            Student newStu = new Student(name,id,address);
            collegeList.add(newStu);        
        }
    
    }
    
    class Student{
        String name;
        Long id;
        String address;
    
        Student(String name,Long id,String address){
        this.name= name;
        this.id = id;
        this.address=address;
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用split()方法破解您的信息

      你要做的是

      String str = new String(information);
      String[] s = str.split(" ", 3);
      

      我在空间基础上拆分,第二个参数 3 意味着你必须分成 3 个字符串,即名称、id、地址

      在此之后您可以获得name via s[0] , id via s[1] and address via s[2] 这样您就可以轻松通过 addStudent() 方法。但是你需要根据addStudent()方法的参数来转换值

      【讨论】:

        猜你喜欢
        • 2020-11-08
        • 2014-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-30
        • 2020-05-14
        相关资源
        最近更新 更多