我正在測試我的大學課程,當用戶輸入「add」時,它將新學生添加到大學課堂中的數組列表中。使用Java將參數添加到arraylist中
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方法是在大專班,看起來像這樣:
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的常見子類型。 –
提供有關您的輸入的一些信息。如何在輸入中分隔名稱,ID和地址?換行符?空白?逗號? – Mohayemin