gpt4 book ai didi

java - 通过 Arraylist 进行查询

转载 作者:行者123 更新时间:2023-12-01 14:36:46 24 4
gpt4 key购买 nike

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

简而言之,我如何搜索抛出 Arraylist存储在 ProcessRecords例如,为 2014 年毕业的学生开设的类(class)?由于某种原因,我似乎无法返回特定年份毕业的学生的记录。

下面是我的代码。

第一类: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);
}
}

第二类:StudentRecord

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包含特定年份毕业的学生的学生记录,然后将整行打印到屏幕上,那就太棒了!我对编程非常陌生,因此我感谢您为我提供的所有帮助。

最佳答案

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++;
}
}
}

关于java - 通过 Arraylist 进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16421884/

24 4 0