0

我正在构建一个程序来读取.txt文件并提取学生数据并将其存储在集合中。然后用户应该能够选择几个不同的查询。我寻求帮助的查询是选择所有毕业的学生,​​例如 2014 年,然后将这些结果打印到屏幕上。

简而言之,我如何搜索将Arraylist存储在ProcessRecords课堂上的学生,例如 2014 年毕业的学生?由于某种原因,我似乎无法返回特定年份毕业的学生的记录。

下面是我的代码。

第一类:ProcessRecords(带有 main 方法)

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

 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 collection
            break;
            //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(); 
            query.getYear(Integer.parseInt(yearsearch));
            //getYear();
            break;
            //Call Query Method to return students who are graduating in the specified year
            //Pass the "yearsearch" variable to the Query class
            case 3:
            System.out.println("What string would you like to search for? \n");
            String lstsearch=preference.next();
            break;
            default:
            System.out.println("Please pick a number between 1 and 3") ;
            break;
            //Call Query Method in the Query Class to return students who have the string in their last name
            //Also 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());

    Query query = new Query(studentRecords);
  }
}

第二类:学生记录

import java.util.*;
public class StudentRecord
{
private int id;
private String last;
private String first;
private int year;

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

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

public int getYear()
{

    return year;
}

第三类:查询

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

public class Query
{
private List<StudentRecord> records;

public Query(List<StudentRecord> records) {
    this.records = records;
}

public void getYear(int yearSearch) {
    //I am trying to create a method to get students who are graduating in a specific year.
    // I want to call this method in the ProcessRecord SwitchCase Case #2
    int count = 0;

    for(StudentRecord record : records) {
        if(record.getYear() == yearSearch) {
           System.out.println(record.toString());
            count++;
        }
    }


}
}

问题

如果您能帮我弄清楚如何Arraylist在学生记录中搜索特定年份毕业的学生,​​然后将整行打印到屏幕上,那就太好了!我对编程很陌生,所以我很感激你能为我提供的所有帮助。

4

2 回答 2

1
public void getYear(int yearSearch) {
//I am trying to create a method to get students who are graduating in a specific year.
// I want to call this method in the ProcessRecord SwitchCase Case #2
int count = 0;

for(StudentRecord record : records) {
    if(record.getYear() == yearSearch) {
       System.out.println(record.getId()+" " + record.getFirst()+ " "+ record.getLast() + "+record.getYear());
        count++;
    }
}
}
于 2013-05-09T13:01:04.600 回答
0

我已将代码插入到我的 IDE 中,有几点引起了我的注意。我已经开始使用您问题中描述的代码,所以我可能会说一些我在评论中已经描述过的东西。

首先,确保导入了正确的类。如果您的 3 个类都在同一个包中,则不需要导入。还要确保你的类在正确的包中。仅将文件放在文件夹中是不够的,在您的import陈述之上,您需要以下内容:

package querytestproject;

import java.io.File;
...

完成后,所有 3 个类都可以识别其他 2 个类并用它做一些事情。

接下来发生的第一件事是:

case 2:
System.out.println("What graduation year would you like to search for? \n");
String yearsearch = preference.next();
query.getYear(Integer.parseInt(yearsearch)); // <------------

query还不知道。你还没有实例化它。AQuery是一个可以帮助您搜索学生列表的对象。您需要为该查询对象提供记录列表以实现此目的。您在 - 方法中执行此操作main。您在那里正确地创建了一个查询对象。但是这里有一个小问题。

当您Query query = new Query(studentRecords);在主方法中执行此操作时,您会在本地实例化查询对象。这意味着您只能在它创建的方法中使用查询对象,即main. 所以,如果你跑AskUserquery那是未知的。

为了解决这个问题(使query全局化,是一种解决方案),在类中实例化query,而不是在方法中。

public class ProcessRecords  {

    Query query;

    public static void AskUser()
    ...

在方法结束时main,替换Query query = new Query(studentRecords);为:

query = new Query(studentRecords);

您会看到弹出错误,因为您尝试在静态方法 ( static main) 中访问非静态对象。

静态 main 方法意味着从这个类创建的所有对象只有一个方法 main。并且可以在不实例化类的情况下调用main方法,直接用类名调用该方法。声明为静态的类的所有实例变量和方法都是全局变量或全局方法。我们可以直接调用它们,而无需从类中创建对象,方法是将其类名放在变量前面,将方法名放在点运算符之间。

因此,我们query像这样创建对象:

static Query query;

运行该文件将显示该文件已正确读取。但是,在那之后,程序停止了。当您浏览 中的所有代码行时main,您看不到对AskUser. 在main方法中设置好所有内容后,是时候询问用户他想要什么了。在main方法中:

...
query = new Query(studentRecords);
AskUser();

您的其余代码是正确的。query正确加载了学生列表,从文件中读取。query包含所有学生。因此,如果您搜索一年,它将通过调用来搜索您刚刚加载的学生列表query.getYear(x)

于 2013-05-09T12:09:25.687 回答