gpt4 book ai didi

java - 需要 txt 文件中的数据(以逗号分隔),以使用现有类中的对象填充数组列表

转载 作者:行者123 更新时间:2023-11-30 04:01:40 25 4
gpt4 key购买 nike

我需要:

  • 从文本文件中获取输入并逐行读取,
  • 获取每一行并将其拆分为学生对象并将这些值发送到数组列表,
  • 将对象的数组列表从 Student 类发送到我的 servlet,
  • 将 servlet 中的数组列表发送到我的 jsp 页面
  • 在 html 表格中显示值。

这是我针对此特定功能的页面。我在这个项目中涉及许多不同的 java 页面、servlet 和 jsp 页面,因此如果您需要其中的任何内容,请直接询问。

文本文件包含按以下顺序排列的学生信息:

Student ID, Last Name, First Name, Quiz Scores 1-5, Makeup Quiz Score, Midterm Score, Problems Score, and Final Exam Score.

文本文件的格式示例如下:

1234,Some,Person,98.0,94.0,97.0,96.0,99.0,0.0,92.0,95.0,99.0 1235,Another,Person,89.0,99.0,87.0,85.0,88.0,0.0,89.0,98.0,99.0 1236,Onemore,Person,89.0,98.0,89.0,98.0,97.0,0.0,79.0,89.0,99.0

但是,我在表格中得到的输出只是第一行,并且全部位于第一个单元格中。

如果你能给我指出正确的方向,我就完全迷失了。

这是我的学生类(class)页面:

public class Student {
private String sid,fnm,lnm,lgrade;
private double q1,q2,q3,q4,q5,qmkup,midt,probs,finex,qavg,cavg;

public Student (){
//empty constructor
sid="";
lnm="";
fnm="";
lgrade="";
q1= 0;
q2= 0;
q3= 0;
q4= 0;
q5= 0;
qmkup= 0;
midt= 0;
probs= 0;
finex= 0;
qavg= 0;
cavg= 0;
}



public String getSid() {
return sid;
}

public void setSid(String sid) {
this.sid = sid;
}

public String getFnm() {
return fnm;
}

public void setFnm(String fnm) {
this.fnm = fnm;
}

public String getLnm() {
return lnm;
}

public void setLnm(String lnm) {
this.lnm = lnm;
}

public String getLgrade() {
return lgrade;
}

public double getQ1() {
return q1;
}

public void setQ1(double q1) {
this.q1 = q1;
}

public double getQ2() {
return q2;
}

public void setQ2(double q2) {
this.q2 = q2;
}

public double getQ3() {
return q3;
}

public void setQ3(double q3) {
this.q3 = q3;
}

public double getQ4() {
return q4;
}

public void setQ4(double q4) {
this.q4 = q4;
}

public double getQ5() {
return q5;
}

public void setQ5(double q5) {
this.q5 = q5;
}

public double getQmkup() {
return qmkup;
}

public void setQmkup(double qmkup) {
this.qmkup = qmkup;
}

public double getMidt() {
return midt;
}

public void setMidt(double midt) {
this.midt = midt;
}

public double getProbs() {
return probs;
}

public void setProbs(double probs) {
this.probs = probs;
}

public double getFinex() {
return finex;
}

public void setFinex(double finex) {
this.finex = finex;
}

public double getQavg() {
calcStudent();
return qavg;
}

public double getCavg() {
return cavg;
}

private void calcStudent(){
double[] qs = { q1, q2, q3, q4, q5, qmkup};
Arrays.sort(qs);
qavg = (qs[2] + qs[3] + qs [4] + qs[5]) / 4.0;

if (qavg >= 89.5 && midt >= 89.5 && probs >= 89.5){
cavg = (qavg+midt+probs) / 3.0;
lgrade = "A";
}else{
cavg = (qavg * .5) + (midt * .15) + (probs * .1) + (finex * .25);
if (cavg >= 89.5){
lgrade = "A";
}else if (cavg >= 79.5){
lgrade = "B";
}else if (cavg >= 69.5){
lgrade = "C";
}else if (cavg >= 59.5){
lgrade = "D";
}else{
lgrade = "F";
}
}
}

@Override
public String toString(){
String s = sid + "," + lnm + "," + fnm + "," +
q1 + "," + q2 + "," + q3 + "," + q4 + "," + q5 + "," +
qmkup + "," + midt + "," + probs + "," + finex;

return s;
}

这是我的java页面:

public class StudentIO {
public static void addStudent(Student s, String path)
throws IOException{
File f = new File(path);
PrintWriter out = new PrintWriter(new FileWriter(f,true));
out.println(s.toString());
out.close();
}

public static ArrayList<Student> getStudentList(String path)
throws IOException {

BufferedReader in = new BufferedReader
(new FileReader(path));

ArrayList<Student> sList = new ArrayList<Student>();
String line = in.readLine();
String data[];


while (line != null){
data = line.split(",");
Student s = new Student();

s.setSid(data[0]);
s.setLnm(data[1]);
s.setFnm(data[2]);
s.setQ1(Double.parseDouble(data[3]));
s.setQ2(Double.parseDouble(data[4]));
s.setQ3(Double.parseDouble(data[5]));
s.setQ4(Double.parseDouble(data[6]));
s.setQ5(Double.parseDouble(data[7]));
s.setQmkup(Double.parseDouble(data[8]));
s.setMidt(Double.parseDouble(data[9]));
s.setProbs(Double.parseDouble(data[10]));
s.setFinex(Double.parseDouble(data[11]));

s.getSid();
s.getLnm();
s.getFnm();
s.getQ1();
s.getQ2();
s.getQ3();
s.getQ4();
s.getQ5();
s.getQmkup();
s.getMidt();
s.getProbs();
s.getFinex();

sList.addAll(Arrays.asList(s));

line = in.readLine();
}
in.close();
return sList;

}

这是我的 servlet:

public class ClassListServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String emsg = "";
String path;
String URL = "/ClassList.jsp";

ArrayList<Student> stulist;

try{
path = getServletContext().getRealPath("/WEB-INF/classlist.txt");
stulist = StudentIO.getStudentList(path);
request.setAttribute("stulist", stulist);
}catch (Exception e){
emsg = "Process error: " +e.getMessage();
URL = "/students.jsp";
}
RequestDispatcher disp = getServletContext().getRequestDispatcher(URL);
disp.forward(request, response);
}

这是 JSP 页面:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Class List</title>
</head>
<body>

<h1>Students on file:</h1>
<table>
<tr>
<th colspan="12"><br>Class List</th>
</tr>
<tr>
<th colspan="1">Student ID</th>
<th colspan="1">Last Name</th>
<th colspan="1">First Name</th>
<th colspan="1">Quiz 1</th>
<th colspan="1">Quiz 2</th>
<th colspan="1">Quiz 3</th>
<th colspan="1">Quiz 4</th>
<th colspan="1">Quiz 5</th>
<th colspan="1">Make-Up Quiz</th>
<th colspan="1">Midterm</th>
<th colspan="1">Problems</th>
<th colspan="1">Final</th>
</tr>
<%

ArrayList<Student> studt = (ArrayList<Student>)
request.getAttribute("stulist");

for (int i = 0; i < studt.size(); i++) {
%>
<tr>
<td><%= studt.get(i) %></td>
</tr>


<% } %>
</table>
<%
//cells and titles for each output field
//every time through loop will process each individual
//student object instead of string objects
%>
</body>

最佳答案

问题出在 getStudentList() 方法中,您不需要每次都创建一个新的 ArrayList

sList = new ArrayList<Student>(); 

必须移出 while 循环

关于java - 需要 txt 文件中的数据(以逗号分隔),以使用现有类中的对象填充数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21844180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com