java - 2.0版本(现在添加查询)

标签 java loops collections

我正在编写一个程序,它将读取文件并提取每个学生的数据。我使用 while 循环和 input.next() 成功完成了此操作。但是,我需要将变量传递到一个集合中来记录每个学生的数据,因此对于每个循环,我想再次将 4 个变量(id、first、last、year)添加到集合中。我应该注意,该集合必须位于不同的类(class)中,并且我必须能够搜索该集合以找到例如今年毕业的所有学生。 如果有人能指出我关于将变量存储在集合中的正确方向,该集合位于不同的类中,对于每个循环。 我知道这是一个基本问题,但我对 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;
   } 

}

谢谢!

最佳答案

更改第二个类:

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

关于java - 2.0版本(现在添加查询),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16408979/

相关文章:

C++ 加密 ascii 值打印数组中的返回键

javascript - 每次调用一个函数你能返回不同的值吗? - JavaScript

java - 迭代器与 For-Each

java - 如何根据整数数组的索引查找对象?

Java 应用程序(处理 IDE)附加窗口在 i3wm 中不可见,常见修复不起作用

java - 如何使用 oracle.jdbc.driver.OracleLog?

java - 如何实现多类应用

php - Laravel 按月收集

java - Ljava.lang.Object;不能转换为 [Ljavax.servlet.http.Cookie;

java - 如何解决不可变的 String[]