2014-10-10 133 views
0

我在uni任務上遇到Java問題。我們已經考慮到具有列爲這樣一組信息文件(還有更多,這僅僅是一個格式化的例子):在數組中存儲字符串並拆分字符串

57363喬伊·賴德DDCPHHCD
72992勞拉Norder HHHDDHHH
71258艾琳在CFCDCCCP

對於我的生活,我無法弄清楚如何將它存儲在一個數組中,並且我需要將它拆分,因爲這些字母需要轉換爲數字並進行平均,然後需要將其存儲到第二陣列。

我是Java的新手,所以很多需要在代碼開始處導入的東西類型對我來說都是未知的,所以在任何回覆中解釋代碼更改都將不勝感激。我到目前爲止的代碼如下:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class StudentGPA_16997761 { 

    public static void main(String[] args) throws FileNotFoundException { 
    Scanner kb = new Scanner(System.in); 

    //get file 
    System.out.print("Please enter the name of the file containing student information: "); 
    String gradeFile = kb.next(); 
    Scanner grades = new Scanner(new File(gradeFile)); 

    if (new File(gradeFile).exists()) { 
     while (grades.hasNextLine()) { 
     System.out.println(grades.nextLine()); 
     } 
    } 

    //student identification number, a first name, a surname, then 8 individual alphabetic characters that represent the 
    //unit grades for the student. Hence, each line of data in the text file represents a student and the grades 
    //they achieved in 8 units of study 

    //need to make array to hold student information 
    //need to make array that holds student id and GPA 

    } 
} 

我知道,它的作品,爲的System.out.println打印出的線條,因爲我希望他們能夠閱讀,但我無法弄清楚如何存儲它們。我認爲我可以讓分割工作,但這仍然需要首先陣列/陣列...

+0

Spit不需要任何ArrayList/Array。 Split可以直接調用String來獲取數組,然後可以使用該數組進行排序。 – 2014-10-10 03:43:34

回答

1

您可以使用分隔符將字符串拆分爲數組。在你的榜樣,如果所有的姓氏和名字不包含空格自己,你可以做到以下幾點:

while (grades.hasNextLine()) { 
    String line = grades.nextLine(); 
    String[] parts = line.split(" "); 

    // get the basics 
    String id = parts[0]; 
    String firstname = parts[1]; 
    String lastname = parts[2]; 

    // extract the grades 
    int size = parts.length - 3; 
    String[] gradelist = new String[size]; 
    System.arraycopy(parts, 3, gradelist, 0, size); 

    // do something with the grades 
} 
+0

謝謝你!這非常有幫助。 – 2014-10-10 06:47:40

0

Java是特別好,在面向對象 - 面向對象編程。輸入文件的每一行都是學生,這是您可以定義的對象的完美示例。讓我們定義一個包含所需信息的學生類:

public class Student{ 
    public final int ID; 
    public final String name; 
    private LinkedList<Character> grades; 
    private double grade;  

    public Student(int i; String n, String[] g){ 
     ID = i; 
     name = n; 
     grades = new LinkedList<Character>(); 
     for(String s : g){ 
      grades.add(s.charAt(0)); 
     } 

     //Do parsing to turn a list of letters into a grade here... 
    } 

    public double getGrade(){ 
     return grade; 
    } 
} 

然後,您可以構建學生在讀取信息時存儲信息。把它放在你當前while循環所在的代碼中。

LinkedList<Student> students = new LinkedList<Student>(); 
while (grades.hasNextLine()) { 
    String[] line = grades.nextLine().split("\\s"); 
    Student s = new Student(Integer.parseInt(line[0]), 
     line[1] + " " + line[2], 
     Arrays.copyOfRange(line, 3, line.length)); 
    students.add(s); 
} 

然後根據需要在學生身上開展工作。

+0

我今天正在回顧這一點,我想知道,它在哪裏增加了新的學生......我不知道如何對此進行說明,所以希望這是有道理的。因爲這是一個while循環,代碼是否會出現問題或者每個新學生是如何「學生」的?我今天正在嘗試編寫代碼,我擔心它會用新數據覆蓋舊數據,因爲它們都是「學生」。或者因爲它是一個新學生,它不會介意同一個名字嗎? – 2014-10-15 04:15:30