ファイルを読み取り、各生徒のデータを抽出するプログラムを作成しています。while ループと input.next() を使用してこれを成功させました。ただし、変数をコレクションに渡して各生徒のデータを記録する必要があるため、ループごとに 4 つの変数 (id、first、last、year) をコレクションに再度追加します。コレクションは別のクラスにある必要があり、たとえば、今年卒業するすべての学生を見つけるには、このコレクションを検索できる必要があることに注意してください。ループごとに、別のクラスにあるコレクションに変数を格納することに関して、誰かが私を正しい方向に向けることができれば。これは基本的な質問であることは承知していますが、私は 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;
}
}
ありがとう!