2016-02-08 50 views
0

我有一個如下所示的文本文件:組織導入的文本文件,字符或字符串?

3333(etc。)是一個學生ID號碼,後面的三個數字是他/她的測試分數。前兩項測試分數爲0.3,第三項爲0.4。零就是分開類。

我有一個緩衝讀取器來輸入所有的數據,但我不知道下一步該怎麼做。我轉換爲字符數組?或者將每個#保存爲一個int並使用數學函數。

我現在所擁有的僅僅是閱讀文件。

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
public class Jose_courseData { 
public static void main(String[]args) { 
    try (BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Jose\\Documents\\Loops\\courseData.txt"))) 
    { 

     String sCurrentLine; 

     while ((sCurrentLine = br.readLine()) != null) { 
      System.out.println(sCurrentLine); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

文本文件看起來像這樣

0.30 0.30 0.40 
    161 
    3333 70 60 50 
    4444 50 50 50 
    5555 80 90 80 
    0 
    162 
    1212 90 85 92 
    6666 60 80 90 
    7777 90 90 90 
    8888 95 87 93 
    9999 75 77 73 
    0 
    263 
    2222 90 65 75 
    8989 60 40 60 
    9090 70 80 30 
    0 

我需要的文本,組織它,和平均分數。這裏是它應該是什麼樣子

Grade Data For Class 161 
ID Programs Midterm Final Weighted Average Programs grade 
-- -------- ------- ----- ---------------- -------------- 
3333 70 60 50 59.00 Pass 
4444 50 50 50 50.00 Fail 
5555 80 90 80 83.00 Pass 
Class Average: 64.00 

Grade Data For Class 162 
ID Programs Midterm Final Weighted Average Programs grade 
-- -------- ------- ----- ---------------- -------------- 
1212 90 85 92 ... Pass 
6666 60 80 90 ... Fail 
7777 90 90 90 ... Pass 
8888 95 87 93 ... Pass 
9999 75 77 73 ... Pass 
Class Average: ... 

Grade Data For Class 263 
ID Programs Midterm Final Weighted Average Programs grade 
-- -------- ------- ----- ---------------- -------------- 
2222 . . . 
8989 . . . 
9090 . . . 
Class Average: ... 

回答

0

您可以使用java.nio中獲取文件的內容作爲字符串列表,以便您可以通過1過濾線1 List類有toArray(T [ ] a)你可以用它將它轉換成一個字符串數組的方法。然後,您可以通過篩選來確定要使用哪一個,以及如何打印它。例如:

package Testers; 

import java.io.File; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.List; 

public class TestClassResults{ 

    public static int testone = 30; //Percentages. Marked as integers. These can also be changed 
    public static int testtwo = 30; 
    public static int testthree = 40; 
    public static List<Float> average; 
    public static int students; 

    public static String[] getAsArray(List<String>ls){ 
     return ls.toArray(new String[]{}); 
    } 

    public static void main(String[] args){ 
    File file = new File("C:\\Users\\Jose\\Documents\\Loops\\courseData.txt"); 
    Path path = Paths.get(file.toURI()); 
    try { 
     List<String> ls = Files.readAllLines(path); 
     String[] array = getAsArray(ls); 
     parseClassRecord(array[1]); 
     parseStudentRecord(array[2]); 
     parseStudentRecord(array[3]); 
     parseClassAverage(); 
     //You can keep parsing class and student records to match whatever you need to do 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
    public static void parseClassRecord(String classno){ 
     System.out.println("Grade Data for Class " + classno); 
     System.out.println("ID Programs Midterm Final Weighted Average Programs grade"); 
     System.out.println("-- -------- ------- ----- ---------------- --------------"); 
    } 

    public static void parseStudentRecord(String studentfacts){ 
     System.out.print(studentfacts + " "); 
     String first = studentfacts.substring(6, 7); 
     String second = studentfacts.substring(9, 10); 
     String third = studentfacts.substring(12, 13); 
     int one = Integer.parseInt(first); 
     int two = Integer.parseInt(second); 
     int three = Integer.parseInt(third); 
     float a = one/testone; 
     float b = two/testtwo; 
     float c = three/testthree; 
     float percent = (a + b + c) /3; 
     System.out.print(percent + " "); 
     if(percent > 0.5){ //If the student scored more than 50% in a test 
      System.out.print("Pass"); 
     }else{ // If they didn't 
      System.out.print("Fail"); 
     } 
     System.out.println(); 
     average.add(Float.valueOf(percent)); 
     students = students + 1; // Also can use students++ (it does the same thing). 
    } 

    public static void parseClassAverage(){ 
     float total = 0; 
     for(int i = 0; i < average.size(); i++){ 
      total = total + average.get(i); 
     } 
     float avg = total/students; 
     System.out.println("Class Average: " + avg); 
     System.out.println(); // Creates a space between the lines. 
    } 

} 

我的方法,你會發現,不使用的BufferedReader,因爲我覺得它讀取一行簡單,但對於一個多行的文件,我覺得它更容易將它們作爲列表,因爲List和Collection API有特殊的方法。