0

我正在编写一个程序,它将读取文件并为每个学生提取数据。我用一个while循环和input.next()成功地做到了这一点。但是,我需要将变量传递到集合中以记录每个学生的数据,因此对于每个循环,我想再次将 4 个变量(id、first、last、year)添加到集合中。我应该注意,该集合必须在不同的班级中,并且我必须能够搜索该集合以查找例如今年毕业的所有学生。如果有人能指出我在将变量存储在集合中的正确方法,该集合在不同的类中,对于每个循环。我知道这是一个基本问题,但我对 Java 很陌生,所以我感谢大家的帮助!

第一堂课是

import java.util.*;
import java.io.*;
import java.lang.*;

  public class ProcessRecords {

   public static void AskUser() 
   throws Exception {
      Scanner preference = new Scanner(System.in);
      //Creating a new scanner will allow us to gather user input

    boolean flag=true; 
    //I will use this for my while loop

    while (flag) {
        System.out.println("What type of Search would you like to run?\n 1)Search for all students\n 2) Search for students graduating in a specific year\n 3)Search for students whose last name begins with a certain string\n");
        int searchType=preference.nextInt();
        //This variable will store what type of query the user would like to run

        switch(searchType) {
            case 1:
            System.out.println("Gathering Records for all students\n");
            //Call Query Method in the Query Class to return all students in the colletion
            case 2
            System.out.println("What graduation year would you like to search for? \n");
            String yearsearch=preference.next();
            //Call Query Method to return students who are graduating in the specified year
            //Pass the "yearsearch" variable to the Query class to run the search
            case 3:
            System.out.println("What string would you like to search for? \n");
            String lstsearch=preference.next();
            //Call Query Method in the Query Class to return students who have the string in their last name
            //I need to pass the "lstsearch" variable to the Query class to search through last   names                

        }
    }
 }

 public static void main(String[] args)
 throws Exception
 {
    Scanner input = new Scanner(new File("students.txt"));
    //This will import the file
    input.nextLine();
    //This will skip the headers in the file
    System.out.println("Processing file now...");
    //Let the user know that the file is being processed
    int id;
    String last;
    String first;
    int year;
    int i=1;
    // Declare variables that we will extract from the file

    //Now we will being processing the file with a while loop

    List<StudentRecord> studentRecords = new ArrayList<StudentRecord>();
    while(input.hasNext())
    {
        id=input.nextInt();
        last=input.next();
        first=input.next();
        year=input.nextInt();
        StudentRecord record = new StudentRecord(id, last, first, year);
        studentRecords.add(record);
        System.out.println(id + "  " + last + "  " + first + "  " + year + "\n");

    }
    System.out.println(" You have successfully read and printed from the file!");
    for (StudentRecord s : studentRecords)
        System.out.println(s.toString());
}
}

下一节课是

   public class StudentRecord{
   public int id;
   public String last;
   public String first;
   public int year;

  public StudentRecord(int d, String lt, String ft, int yr){
      id=d;
      last=lt;
      first=ft;
      year=yr;
  }

   public String toString()
   {
       return id + "  " + last + "  " + first + "  " + year;
   } 

}

谢谢!

4

2 回答 2

1

更改第二类:

public class StudentRecord
{
    public int id;
    public String last;
    public String first;
    public int year;

    public StudentRecord(int d, String lt, String ft, int yr)
    {
        id=d;
        last=lt;
        first=ft;
        year=yr;
    }

    public string toString()
    {
        return id + "  " + last + "  " + first + "  " + year;
    } 
}

该方法称为构造函数,您可以使用它创建此类的实例。

在您的第二个类中,在循环运行时,您可以通过将参数传递给构造函数来为每个条目创建具有实际值的新 StudentRecord 对象:

    List<StudentRecord> studentRecords = new ArrayList<StudentRecord>();
    while(input.hasNext())
    {
        id=input.nextInt();
        last=input.next();
        first=input.next();
        year=input.nextInt();
        StudentRecord record = new StudentRecord(id, last, first, year);
        studentRecords.Add(record);
        System.out.println(id + "  " + last + "  " + first + "  " + year + "\n");

    }

ArrayList 将为您提供所有 StudentRecord 对象的存储。

如果您覆盖 StudentRecord 对象的 toString 方法(如我上面所做的那样),您可以循环打印所有学生记录到控制台:

for (StudentRecord s : studentRecords)
    System.out.println(s.toString());
于 2013-05-06T23:51:25.283 回答
0

制作 StudentRecord 对象的 ArrayList 有什么问题吗?

public class StudentRecord {
    public int id;
    public String last;
    public String first;
    public int year;

    public StudentRecord(int id, String last, String first, int year) {
        this.id = id;
        this.last = last;
        this.first = first;
        this.year = year;
    }
}

然后在您从文件中获取值之后:

ArrayList<StudentRecord> studentRecords = new ArrayList<StudentRecord>();

//...

id = input.nextInt();
last = input.next();
first = input.next();
year = input.nextInt();

studentRecords.add(new StudentRecord(id, last, first, year));

//...
于 2013-05-06T23:48:47.013 回答