[英]Comparing an Arraylist to a String
我正在嘗試比較用戶在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”開頭,那么我想打印它。 感謝您的所有幫助!
public static StudentRecord searchRecords(Collection<StudentRecord> records, String lastName)
{
for (StudentRecord record : records)
{
if (lastName.equalsIgnoreCase(record.getLast()))
return record;
}
return null;
}
這將在您的數組中搜索適當的記錄。 如果存在,則返回該記錄;否則返回null
。 如果將來遇到此類問題,可以隨時咨詢適當的javadoc,以獲取可用於比較對象的某些方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.