我正在编写一个程序,它将读取文件并为每个学生提取数据。我用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;
}
}
谢谢!
答案 0 :(得分: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());
答案 1 :(得分: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));
//...