[英]Perform String Append and Arithmetic Operations on ArrayList<String> elements
我對Java非常陌生,因此請忽略我的明顯錯誤。 我有一個txt文件,其格式如下:
Student Name,Mathmatics_Marks,Physics_Marks,Chemistry_Marks,Biology_Marks
A,10,20,30,40
B,15,15,48,69
C,45,48,48,79
D,48,15,12,55
所需輸出:
I need to do the following output format from the above txt files:
Student Name (Appended with "Student:" prefix)
Pass/Fail (Appended with "Exam Status:" prefix)
Average_Marks (Appended with "Average_Marks:" prefix)
Maximum_Marks(Appended with "Maximum_Marks:" prefix)
Minimum_Marks(Appended with "Minimum_Marks:" prefix)
For example:
Student: A
Exam Status: Pass
Average_Marks:25
Maximum_Marks:40
Minimum_Marks:10
<<so on for other students>>
...........................
...........................
...........................
...........................
所需輸出的邏輯/算法:
1) If (Mathmatics_Marks+Physics_Marks+Chemistry_Marks,Biology_Marks)=>100 then Pass else Fail
2) Find out the average marks of student.
3) Write Maximum marks
4) Write Minimum marks
我的方法:1-我可以使用以下代碼將數據加載到ArrayList並從txt文件打印數據, 但無法實現所需的輸出 :
public static void main(String[] args)
{
//Input file path
String fileToParse = "C:\\Users\\DELL-PC\\Desktop\\Analysis.txt";
BufferedReader fileReader = null;
//Delimiter Declaration
final String DELIMITER = ",";
try
{
List<String> Student_log= new ArrayList<String>();
String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(fileToParse));
//Reading the file line by line
while ((line = fileReader.readLine()) != null)
{
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
for(String token : tokens)
{
//Print all tokens
/// Array Initialization Part
System.out.println(token);
Student_log.add(token);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally
{
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
請幫助我的代碼。
您需要初始化一些變量以計算平均值,最小值,最大值等。然后在while循環內部,從每行中檢索值並進行計算。
Example Code:
while ((line = fileReader.readLine()) != null) {
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
name = tokens[0];
math = Integer.valueOf(tokens[1]);
physics =Integer.valueOf(tokens[2]);
chem = Integer.valueOf(tokens[3]);
bio = Integer.valueOf(tokens[4]);
if (math + physics + chem + bio > 100) {
pass = "Pass";
} else {
pass = "Fail";
}
System.out.println("Student: "+ name);
System.out.println("Exam Status: "+ pass);
}
public static void main(String[] args)
{
//Input file path
String fileToParse = "C:\\Users\\DELL-PC\\Desktop\\Analysis.txt";
BufferedReader fileReader = null;
//Delimiter Declaration
final String DELIMITER = ",";
try
{
List<String> Student_log= new ArrayList<String>();
String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(fileToParse));
//Reading the file line by line
while ((line = fileReader.readLine()) != null)
{
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
student_log = new ArrayList<>();
for(String token : tokens)
{
//Print all tokens
/// Array Initialization Part
Student_log.add(token);
}
// calculate average
int sum = 0;
for(int i=1; i<student_log.size();i++){
sum = sum + Integer.parseInt(student_log.get(i));
}
double average = sum/(student_log.size()-1);
String result = "fail";
if(average > 100){
result = "pass";
}
int max = Integer.parseInt(student_log.get(1));
//Calculate max
for(int i=1; i<student_log.size(); i++){
if(max < Integer.parseInt(student_log.get(i)))
max = Integer.parseInt(student_log.get(i));
}
//Calculate min
int min = Integer.parseInt(student_log.get(1));
//Calculate max
for(int i=1; i<student_log.size(); i++){
if(min > Integer.parseInt(student_log.get(i)))
min = Integer.parseInt(student_log.get(i));
}
System.out.println("Student: " + student_log.get(0));
System.out.println("Exam_status: " + result);
System.out.println("Average marks: " +average);
System.out.println("Maximum marks: " +max);
System.out.println("Minimum marks: " +min);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally
{
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這將很好地工作,但是事實上,您將所有內容存儲在字符串數組列表中。 我建議您創建一個類的模型,名稱為字符串,所有其他標記為整數。 那將是一個很好的編程習慣,但是這是一個簡單的程序,在這種情況下,您可以使用Integer.parseInt()。
但是,我想在這里指出一點。 計算最大值和最小值時,切勿將0賦給初始最大值和最小值變量。 通常很多這樣做。 因為,在某些情況下會出現負數(在這種情況下不存在)。 但是,在這種情況下,如果我們將min設為0,則永遠不會進入循環。
您可能有一些問題。 首先,您需要進行一些計算以計算通過/失敗,最大值,最小值等。因此,您需要首先遍歷這些值以確定該值,然后將其轉換為整數,依此類推。 然后,計算出的值就是您要打印的值,但是要手動添加一些其他文本,而不是在該循環中。 因此,一些Student_log.add(text); 語句,每次計算一個。 也跳過文本的第一行。
您需要忽略第一行並為您的4個選項添加方法,例如
getAverageMark(List marks) {
//find average
}
清單標記-特定學生的所有標記,您必須閱讀每行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.