我正在尝试比较用户在 ProcessRecords 类中的案例 3 中输入的字符串。然后我试图将该字符串与 Arraylist 中的每个姓氏进行比较,如果记录与用户的输入匹配,那么我想打印它。我不知道如何做到这一点,但我会很感激任何指导。
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(4) Exit\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");
//Query.getAll();
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");
int yearsearch=preference.nextInt();
Query.getYears(yearsearch);
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();
Query.getLast(lstsearch);
break;
case 4:
System.out.println("You have chosen to exit the program. Thank you!");
flag=false;
break;
default:
System.out.println("Please pick a number between 1 and 4") ;
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 List<StudentRecord> studentRecords;
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;
}
public String getLast()
{
return last;
}
}
+++++++++++++++++++++++++++++++++++++++ 下一节
import java.util.*;
import java.io.*;
public class Query
{
static List<StudentRecord> records;
public List<StudentRecord> studentRecords;
public Query(List<StudentRecord> records) {
this.records = records;
}
public static void getYears(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;
System.out.println("ID" +" " +"Last" + " " + "First" + " " + "Year \n");
for(StudentRecord record : records) {
if(record.getYear() == yearSearch) {
System.out.println((record.toString()));
count++;
}
System.out.printf("\n");
}
}
public static void getLast(String lstsearch){
int count =0;
System.out.println("ID" +" " +"Last" + " " + "First" + " " + "Year \n");
int sizes= lstsearch.length();
System.out.println(lstsearch);
for(StudentRecord record : records) {
System.out.println(count);
if(record.getLast() == lstsearch) {
System.out.println((record.toString()));
count++;
}
}
}
public void getAll(){
for (StudentRecord s : studentRecords)
System.out.println(s.toString());
}
}
再一次,我正在寻找帮助搜索数组列表,如果数组列表中的姓以字符串开头,即“g”,那么我想打印它。感谢您的所有帮助!