2013-05-07 83 views
0

我正在構建一個程序來讀取.txt文件並提取學生數據並將其存儲在一個集合中。那麼用戶應該能夠選擇幾個不同的查詢。我要求幫助的查詢是選擇所有畢業的學生,​​例如2014年,然後將這些結果打印到屏幕上。通過不同類別的Arraylist調用和搜索

簡而言之,我如何搜索存儲在ProcessRecords類中的Arralist,例如,2014年畢業的學生?我只是不明白如何從不同的課程中調用它。

下面是我的代碼:

頭等艙:隨着主要方法

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()); 
} 
} 


二等學生記錄

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; 
} 
} 


第三類(一個我要求幫助的)

import java.util.*; 
import java.io.*; 

    public class Query 
{ 
    //public static ProcessRecords studentrecord = new ProcessRecords(); 
    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; 
    } 
    } 

增添了新的職位!

+0

你忘了? eak你的案例。如果'searchType'爲1,則所有三種情況下的代碼將被執行。 – 2013-05-07 06:37:18

+0

謝謝!有沒有可能幫助我收集ListArray中的所有記錄,以便在特定年份畢業的學生? – user2263142 2013-05-07 14:04:47

回答

0

你必須做出studentRecords靜態變量或實例變量,

把它像這樣,上述情況主要有:

public static List<StudentRecord> studentRecords ; 
public static void main(String[] args) 
throws Exception{ 

studentRecords = new ArrayList<StudentRecord>(); 

然後就這樣稱呼它:

ProcessRecords.studentRecords 
0

您的代碼有多個問題。

  1. 公共變量氣餒,getter/setter方法應被用來訪問對象的成員。 Eclipse alt+s, r會爲你生成它們

  2. 你的變量名稱是誤導性的。您在Query中訪問的列表是什麼?

  3. 你的類的名字也是(至少!)誤導。 ProcessRecords作爲一個行動,特別是似乎不好。在這種情況下,班級不應該是名詞,比如RecordsProcessor?它不應該暗示它實際在做什麼嗎? StudentYearSearcher

  4. 您不會拋出該列表,而是將它作爲參數傳遞(它是引用),或者以某種方式訪問​​它。你拋出異常

回答你的問題

有多種方法可以做到這一點。一種方法是使用單例模式並使列表靜態可訪問。就像這樣:

class StudentRecord { 
    static List<StudentRecord> studentRecords; 

    List<StudentRecord> getStudentRecords() { 
    if (studentRecords == null) studentRecords= new ArrayList<StudentRecord>(); 
    return studentRecords; 
    } 

    //the reest of the class 
} 
0

最簡單的解決辦法是通過整個列表到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(); 

     // ... 
0

讓你的查詢類是這樣的:

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); 
    } 
} 
+0

爲什麼在編譯ProcessRecords類時找不到符號-Variable StudentRecords?我在上次編輯中更新了代碼 – user2263142 2013-05-07 06:47:50

+0

AskUser()方法中不存在studentRecords變量您需要創建Query對象而你可以訪問studentRecords列表,你可以在main中創建列表之後(在整個循環之後)做到這一點,或者你可以讓studentRecords成爲一個實例變量並以此方式訪問它,或者你可以使查詢對象是一個實例變量,你也可以在AskUser()中訪問它。 – 2013-05-07 06:54:09

+0

再次查看你的代碼最簡單的方法可能是將查詢對象傳遞給AskUser方法。在你建立了studentRecords變量之後(在你的成功消息的正上方或下方),你必須將AskUser改爲「public static void AskUser(Query query)」。 – 2013-05-07 07:02:18