所以我试图读取我从results.txt文件中获取的值并根据CGPA格式计算它们,但似乎我尝试的方法不起作用。我想知道是否有人可以帮助我,这是我的客户服务器任务。计算部分位于View()方法下。 PS抱歉,如果我的代码太难阅读,我仍然不习惯使用java,这是我第一次在这里发帖
import java.net.*;
import java.io.*;
import java.lang.*;
public class MultiClientHandler1 extends Thread {
private Socket client;
private BufferedReader in, in2,in3;
private PrintWriter out, out2;
private String received, data,result;
private String array[], array2[], array3[],array4[];
private int found=0;
public MultiClientHandler1(Socket socket) {
client = socket;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
out2 = new PrintWriter(new FileWriter("students.txt", true));
} catch(IOException e) {
e.printStackTrace();
}
}
public void Register() {
String reply="";
array = received.split(":");
if(array[1].length() != 7)
reply = "Err1";
else if(array[2].length() < 5)
reply = "Err2";
else if(array[3].length() < 5)
reply = "Err3";
else {
reply= "+OK";
out2.println(array[1]+":"+array[2]+":"+array[3]);
out2.close();
}
out.println(reply);
}
public void login() throws IOException {
in2 = new BufferedReader(new FileReader("students.txt"));
while((data = in2.readLine()) != null) {
array3=data.split(":");
if((array2[1].equals(array3[0])) && (array2[2].equals(array3[1]))) {
out.println("Login successfully");
found=1;
break;
}
}
if(found==0)
out.println("Login Error");
}
public void View() throws IOException {
String grade = "", cgpa = "";
int IntValue = 0, IntCH = 0, IntCGPA = 0, IntArray = 0;
in3 = new BufferedReader(new FileReader("results.txt"));
while((result = in3.readLine()) != null) {
array4=result.split(":");
if((array2[1].equals(array4[0])) && (array2[2].equals(array4[1]))) {
grade += array4[2] + " " + array4[3] + " " + array4[4] + "...........";
found = 1;
if(array4[3] == "A") {
IntArray = Integer.parseInt(array4[3]);
IntValue = IntArray * 4;
} else if(array4[3] == "B") {
IntArray = Integer.parseInt(array4[3]);
IntValue = IntArray * 3;
}
IntCH += IntArray;
IntCGPA = IntValue/IntCH;
out.println(IntCGPA);
}
if(found==0)
out.println("No Records");
}
out.println(grade);
}
public void run() {
try {
do {
found=0;
received = in.readLine();
array2=received.split(":");
if (received.equals("QUIT")) break;
switch(received.charAt(0)) {
case 'R':
Register();
break;
case 'L':
login();
break;
case 'V':
View();
break;
}
} while (!received.equals("QUIT"));
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
if(client != null) {
System.out.println("Closing down connection...");
client.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
these are the content in the results.txt file
0118210:1801:XDCS1094:A:4
0118210:1801:XDCS2034:B+:4
0118210:1801:XDCS1043:C:3
0118024:1801:XDCS1094:B:4
0118024:1801:XDCS2094:A:4