简体   繁体   中英

Simple Java program Arraylist

I'm trying to make a simple program that demonstrates the use of ArrayLists of objects. I want to be able to input the details to be stored in the objects as well as searching for and printing objects.

I'm doing it on Students I have my Student class made and I have started my studentTest class but I'm looking for help with input to start with.

This is what I have so far:

package student;

public class Student {

    private String studentName;
    private String studentNo;
    private String email;
    private int year;

    public Student() {
      /**studentName = null;
        *studentNo = null;
        *email = null;
        *year = -1;
        */
    }

    public Student(String nName, String nNum, String nEmail, int nYr) {
        this.studentName = nName;
        this.studentNo = nNum;
        this.email = nEmail;
        this.year = nYr;
    }

    public void setStudentName(String newStudentName) {
        studentName = newStudentName;
    }

    public void setStudentNo(String newStudentNo) {
        studentNo = newStudentNo;
    }

    public void setEmail(String newEmail) {
        email = newEmail;
    }

    public void setYear(int newYear) {
        year = newYear;
    }

    public String getStudentName() {
        return studentName;
    }

    public String getStudentNo() {
        return studentNo;
    }

    public String getEmail() {
        return email;
    }

    public int getYear() {
        return year;
    }


    }
}

package student;
import java.util.ArrayList;
import java.util.Scanner;

public class studentTest {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Student student1 = new Student();
        Student student2 = new Student();
        Student student3 = new Student();
        Student student4 = new Student();
        Student student5 = new Student();


    }
}

Have a look at the document for Scanner .

From the code that you have done so far in studentTest it looks like you are taking all the inputs from the user.

You basically need to input all data in a loop:

// create ArrayList
for (int i=0; i < 5; i++) {
   String studentName = input.nextLine();
   ....

   Student student = new Student();
   student.setStudentName();
   ...

   // add student to arraylist
}

I think that you will benefit from filling in the bits I've left out.

I think he just want to populate the Student class something like this.

 while(true){
    Scanner input = new Scanner(System.in);

    Student student1 = new Student();
    int year= input.nextInt();
    String studentName=input.nextLine();
    studentNo=input.nextLine();
    String email=input.nextLine();

    student1.setStudentName(studentName);
    student1.setStudentNo(studentNo);
    student1.setEmail(email);
    student1.setYear(year) 

    int year= input.nextInt();
    String studentName=input.nextLine();
    studentNo=input.nextLine();
    String email=input.nextLine();



      break; // at the last position
      }

You may want to update the main method as below:

public static void main(String[] args) {
    List<Student> students = new ArrayList<Student>();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter number of students");
    int countStudents = input.nextInt();

    for(int i=0; i< countStudents; i++){
          Student student = new Student();
          System.out.println("Enter details for student"+i);

          System.out.println("Enter name");
          student.setStudentName(input.next()); 

          System.out.println("Enter Number");
          student.setStudentNo(input.next()); 

          System.out.println("Enter email");
          student.setEmail(input.next()); 

          System.out.println("Enter year");
          student.setYear(input.nextInt()); 
          students.add(student);
    }
    //Your list of students is populated now
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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