2013-05-07 335 views
0

我想比較用戶在ProcessRecords類的情況3中輸入的字符串。然後我試圖將該字符串與Arraylist中的每個姓氏進行比較,如果記錄匹配用戶的輸入,那麼我想打印它。我不知道如何做到這一點,但我會很感激任何指導。將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()); 
} 

} 

再次,我正在尋找通過arraylist搜索的幫助,如果數組列表中的姓氏以字符串開頭,即「g」,那麼我想打印它。感謝您的幫助!

+1

請將代碼簡化爲您需要解決的特定問題。循環ArrayList非常容易,但我不確定這是否是您正在尋找的。 – 2013-05-07 21:33:31

回答

0
public static StudentRecord searchRecords(Collection<StudentRecord> records, String lastName) 
{ 
    for (StudentRecord record : records) 
    { 
     if (lastName.equalsIgnoreCase(record.getLast())) 
      return record; 
    } 
    return null; 
} 

這將通過您的數組搜索適當的記錄。如果它存在,則返回該記錄 - 否則返回null。如果將來有這樣的問題,您可以隨時諮詢appropriate javadoc以獲取可用於比較對象的一些方法。