从数据文件向java队列添加对象
我试图从一个数据文件向队列中添加对象,该文件由文本组成,文本由一个人的名字和他们的6个测验分数(即:Jimmy,100100)组成。我使用FileReader访问数据文件,并使用BufferReader读取数据文件的每一行,然后使用“,”删除器标记每一行,以划分名称和测验分数。基于我认为我的教授要求的是为每个学生创建一个队列对象。任务说 使用循环每次读取一行文本文件的内容。在这个循环中,为每一行读取调用processInputData方法。此方法返回相应的学生对象。将此学生对象添加到studentQueue 如果有人能给我指出正确的方向那就太好了!以下是我目前的代码:从数据文件向java队列添加对象,java,for-loop,queue,bufferedreader,filereader,Java,For Loop,Queue,Bufferedreader,Filereader,我试图从一个数据文件向队列中添加对象,该文件由文本组成,文本由一个人的名字和他们的6个测验分数(即:Jimmy,100100)组成。我使用FileReader访问数据文件,并使用BufferReader读取数据文件的每一行,然后使用“,”删除器标记每一行,以划分名称和测验分数。基于我认为我的教授要求的是为每个学生创建一个队列对象。任务说 使用循环每次读取一行文本文件的内容。在这个循环中,为每一行读取调用processInputData方法。此方法返回相应的学生对象。将此学生对象添加到studen
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Test {
public static void main(String[] args) {
// Create an empty queue of student objects
LinkedList<Student> studentQueue;
studentQueue = new LinkedList<Student>();
// Create an empty map of Student objects
HashMap<String, Student> studentMap = new HashMap<String, Student>();
System.out.printf("Initial size = %d\n", studentMap.size());
// Open and read text file
String inputFileName = "data.txt";
FileReader fileReader = null;
// Create the FileReader object
try {
fileReader = new FileReader(inputFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// BufferReader to read text file
BufferedReader reader = new BufferedReader(fileReader);
String input;
// Read one line at a time until end of file
try {
input = reader.readLine();
while (input != null) {
processInputData(input);
input = reader.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
// Close the input
try {
fileReader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
// Tokenize the data using the "," as a delimiter
private static void processInputData(String data) {
StringTokenizer st = new StringTokenizer(data, ",");
String name = st.nextToken();
String homework1 = st.nextToken();
String homework2 = st.nextToken();
String homework3 = st.nextToken();
String homework4 = st.nextToken();
String homework5 = st.nextToken();
String homework6 = st.nextToken();
// Using the set methods to correspond to the Student object
Student currentStudent = new Student(name);
currentStudent.setHomework1(Integer.parseInt(homework1));
currentStudent.setHomework2(Integer.parseInt(homework2));
currentStudent.setHomework3(Integer.parseInt(homework3));
currentStudent.setHomework4(Integer.parseInt(homework4));
currentStudent.setHomework5(Integer.parseInt(homework5));
currentStudent.setHomework6(Integer.parseInt(homework6));
System.out.println("Input File Processing...");
System.out.println(currentStudent);
}
}
导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.LinkedList;
导入java.util.StringTokenizer;
公开课考试{
公共静态void main(字符串[]args){
//创建学生对象的空队列
LinkedList学生队列;
studentQueue=newlinkedlist();
//创建学生对象的空映射
HashMap studentMap=新建HashMap();
System.out.printf(“初始大小=%d\n”,studentMap.size());
//打开并读取文本文件
字符串inputFileName=“data.txt”;
FileReader FileReader=null;
//创建FileReader对象
试一试{
fileReader=新的fileReader(inputFileName);
}catch(filenotfounde异常){
e、 printStackTrace();
}
//BufferReader读取文本文件
BufferedReader reader=新的BufferedReader(文件读取器);
字符串输入;
//每次读取一行,直到文件结束
试一试{
输入=reader.readLine();
while(输入!=null){
processInputData(输入);
输入=reader.readLine();
}
}
捕获(IOE异常){
e、 printStackTrace();
}
//关闭输入
试一试{
fileReader.close();
}
捕获(IOE异常){
e、 printStackTrace();
}
}
//使用“,”作为分隔符标记数据
私有静态void processInputData(字符串数据){
StringTokenizer st=新的StringTokenizer(数据,“,”);
String name=st.nextToken();
字符串homework1=st.nextToken();
字符串homework2=st.nextToken();
字符串homework3=st.nextToken();
字符串homework4=st.nextToken();
字符串homework5=st.nextToken();
字符串homework6=st.nextToken();
//使用集合方法对应于学生对象
学生当前学生=新学生(姓名);
currentStudent.setHomework1(Integer.parseInt(homework1));
currentStudent.setHomework2(Integer.parseInt(homework2));
currentStudent.setHomework3(Integer.parseInt(homework3));
currentStudent.setHomework4(Integer.parseInt(homework4));
currentStudent.setHomework5(Integer.parseInt(homework5));
currentStudent.setHomework6(Integer.parseInt(homework6));
System.out.println(“输入文件处理…”);
System.out.println(当前学生);
}
}
您的问题的一个可能的解决方案是在processInputData(..)中返回学生。
在while循环中
while (input != null) {
studentQueue.add(processInputData(input));
input = reader.readLine();
}
还要尝试更好地管理try catch
块,因为如果fileReader
引发异常,那么代码将继续运行,并可能引发一个您不处理的nullPointerException
try{
fileReader = new FileReader(inputFileName);
BufferedReader reader = new BufferedReader(fileReader);
}catch(IOException ex){
//handle exception;
}finally{
// close resources
}
您必须将您创建的学生添加到列表中!教授可能想让你使用while/for循环来读取离线标记器中的标记。@nachokk-我知道我可以手动将它们添加到代码中,但是否可以在我的第二次“尝试”(每次读取一行…)中将它们添加到我的列表中。看起来这样做有效,实际上我还有一个后续问题。所以现在我尝试将这些学生对象添加到hashmap中,使用学生的名字作为键,但没有成功。我尝试放置
studentMap.add(processInputData(input))代码>在while循环中,并且已显示为空。如何将特定键(名称)插入空哈希集中?@jnguenstudentMap。(currentStudent.getName(),currentStudent)代码>实际上它是一个hashMap而不是一个hashSet
try{
fileReader = new FileReader(inputFileName);
BufferedReader reader = new BufferedReader(fileReader);
}catch(IOException ex){
//handle exception;
}finally{
// close resources
}