Java 电话及;在不同类中搜索Arraylist
我正在构建一个程序来读取一个.txt文件,提取学生数据并将其存储在一个集合中。然后,用户应该能够选择几个不同的查询。我请求帮助的问题是选择所有毕业的学生,例如2014年,然后将这些结果打印到屏幕上 简言之,我如何为2014年毕业的学生搜索存储在ProcessRecords类中的Arralist?我只是不明白如何从另一个类中调用它 下面是我的代码: 第一类:使用主方法Java 电话及;在不同类中搜索Arraylist,java,class,collections,iterator,Java,Class,Collections,Iterator,我正在构建一个程序来读取一个.txt文件,提取学生数据并将其存储在一个集合中。然后,用户应该能够选择几个不同的查询。我请求帮助的问题是选择所有毕业的学生,例如2014年,然后将这些结果打印到屏幕上 简言之,我如何为2014年毕业的学生搜索存储在ProcessRecords类中的Arralist?我只是不明白如何从另一个类中调用它 下面是我的代码: 第一类:使用主方法 import java.util.*; import java.io.*; import java.lang.*; public
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");
Query query = new Query(studentRecords);
int searchType=preference.nextInt();
//How would I throw an exception here if the user doesn't enter a number or enters a number less than 1 or great than 4
//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
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
//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());
}
}
三等舱(我请求帮助的那一个)
import java.util.*;
导入java.io.*;
公共类查询
{
//public static ProcessRecords studentrecord=new ProcessRecords();
私人名单记录;
公开查询(列表记录){
这个.记录=记录;
}
公共int getYear(int yearSearch){
整数计数=0;
用于(StudentRecord记录:记录){
if(record.getYear()==yearSearch){
计数++;
}
}
返回计数;
}
}
添加了一个新帖子 必须使studentRecords成为静态变量或实例变量 这样说吧,在主体之上:
public static List<StudentRecord> studentRecords ;
public static void main(String[] args)
throws Exception{
studentRecords = new ArrayList<StudentRecord>();
您的代码有多个问题。
alt+s,r
将为您生成它们ProcessRecords
作为一种行为,似乎特别糟糕。在这种情况下,类不应该是一个名词吗,比如RecordsProcessor
?它不应该暗示它实际上在做什么吗<代码>学生年搜索器class StudentRecord {
static List<StudentRecord> studentRecords;
List<StudentRecord> getStudentRecords() {
if (studentRecords == null) studentRecords= new ArrayList<StudentRecord>();
return studentRecords;
}
//the reest of the class
}
class StudentRecord{
静态列表记录;
列出getStudentRecords(){
如果(studentRecords==null)studentRecords=new ArrayList();
归还录音带;
}
//这个班的学生
}
最简单的解决方案是将整个列表传递给getYear
方法:
public static int getYear(List<StudentRecord> studentRecords, int yearsearch) {
// ProcessRecords processRecords = new ProcessRecords(); <- don't need it
int getYear= yearsearch;
Iterator itr = studentRecords.iterator();
// ...
publicstaticintgetyear(列表studentRecords,intyearsearch){
//ProcessRecords=new ProcessRecords();使您的查询类如下所示:
ProcessRecords.studentRecords
public class Query {
private List<StudentRecord> records;
public Query(List<StudentRecord> records) {
this.records = records;
}
public int getYear(int yearSearch) {
int count = 0;
for(StudentRecord record : records) {
if(record.getYear() == yearSearch) {
count++;
}
}
return count;
}
public int otherQuery() {
// code for another query
}
}
公共类查询{
私人名单记录;
公开查询(列表记录){
这个.记录=记录;
}
公共int getYear(int yearSearch){
整数计数=0;
用于(StudentRecord记录:记录){
if(record.getYear()==yearSearch){
计数++;
}
}
返回计数;
}
public int otherQuery(){
//另一个查询的代码
}
}
然后在你的主课上:
import java.util.*;
import java.io.*;
import java.lang.*;
public class ProcessRecords {
public static void AskUser(Query query) throws Exception {
// all the code you have right now except the line where you
// create a new Query object
}
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); // we've moved this out of AskUser method to here
// now we call the AskUser method and pass it this query object we just
// created so it can have access to it, meaning inside the AskUser method we can
// say things like 'query.getYear(2014);'
AskUser(query);
}
}
import java.util.*;
导入java.io.*;
导入java.lang.*;
公共类处理记录{
公共静态void AskUser(查询)引发异常{
//你现在拥有的所有代码,除了你
//创建一个新的查询对象
}
公共静态void main(字符串[]args)引发异常{
扫描仪输入=新扫描仪(新文件(“students.txt”);
//这将导入该文件
input.nextLine();
//这将跳过文件中的标题
System.out.println(“正在处理文件…”);
//让用户知道文件正在处理中
int-id;
最后一串;
先串;
国际年;
int i=1;
//声明我们将从文件中提取的变量
//现在我们将使用while循环处理该文件
List studentRecords=new ArrayList();
while(input.hasNext()){
id=input.nextInt();
last=input.next();
first=input.next();
年份=input.nextInt();
StudentRecord=新的StudentRecord(id、上一个、第一个、年份);
添加(记录);
System.out.println(id+“”+最后一个+“”+第一个+“”+年份+“\n”);
}
System.out.println(“您已成功读取并打印了该文件!”);
用于(studentRecords:studentRecords){
System.out.println(s.toString());
}
Query Query=new Query(studentRecords);//我们已经将它从AskUser方法移到这里
//现在我们调用AskUser方法,并将我们刚刚创建的查询对象传递给它
//创建,以便它可以访问它,这意味着在AskUser方法中,我们可以
//比如说“query.getYear(2014);”
AskUser(查询);
}
}
您忘记了中断您的案例
s。如果searchType
为1,则所有三个案例中的代码都将被执行。谢谢!您是否有机会帮我收集特定年份毕业学生列表中的所有记录?为什么会这样说“编译ProcessRecords类时找不到符号-变量StudentRecords?我在上一次编辑中更新了代码。StudentRecords变量在AskUser()方法中不存在。您需要在访问StudentRecords列表时创建查询对象。您可以在创建列表后(在整个循环之后)立即创建该对象。”或者你可以让studentRecords成为一个实例变量,并以这种方式访问它。或者你可以让Query对象成为一个实例变量,你也可以以这种方式在AskUser()中访问它。再次查看你的代码,最简单的方法可能是通过
public class Query {
private List<StudentRecord> records;
public Query(List<StudentRecord> records) {
this.records = records;
}
public int getYear(int yearSearch) {
int count = 0;
for(StudentRecord record : records) {
if(record.getYear() == yearSearch) {
count++;
}
}
return count;
}
public int otherQuery() {
// code for another query
}
}
import java.util.*;
import java.io.*;
import java.lang.*;
public class ProcessRecords {
public static void AskUser(Query query) throws Exception {
// all the code you have right now except the line where you
// create a new Query object
}
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); // we've moved this out of AskUser method to here
// now we call the AskUser method and pass it this query object we just
// created so it can have access to it, meaning inside the AskUser method we can
// say things like 'query.getYear(2014);'
AskUser(query);
}
}