2013-04-18 31 views
0

所以,我在試圖做的是我從一個文本文件中的信息過濾掉信息,如何使用useDelimiter

 For example, 134567H;Gabriel;24/12/1994;67;78;89 

然後我顯示只有管理員號碼這是第一位的,但不整行在下拉列表中。因此,這裏是我的代碼:

public static String[] readFile(){ 
    String file = "output.txt"; 
    ArrayList <String> studentList = new ArrayList <String>(); 
    try{ 
    FileReader fr = new FileReader(file); 
    Scanner sc = new Scanner(fr); 
    sc.useDelimiter(";"); 

    while(sc.hasNextLine()){ 
     studentList.add(sc.nextLine()); 
    } 

    fr.close(); 
    }catch(FileNotFoundException exception){ 
     System.out.println("File " + file + " was not found"); 
    }catch(IOException exception){ 
     System.out.println(exception); 
    } 
    return studentList.toArray(new String[studentList.size()]); 
} 

這就是我如何填充下拉列表:

public void populate() { 
    String [] studentList ; 
    studentList = Question3ReadFile.readFile(); 

    jComboBox_adminNo.removeAllItems(); 

    for (String str : studentList) { 
     jComboBox_adminNo.addItem(str); 
    } 
} 

不過,現在我的問題是,在下拉列表中選擇是顯示了從整條生產線文本文件。它不顯示管理員號碼。我已經用useDelimiter嘗試過了。我應該用那個嗎?

任何幫助,將不勝感激。提前致謝。

Rince幫助檢查。

public class Question3ReadFile extends Question3 { 

private String adminNo; 

public Question3ReadFile(String data) { 
    String[] tokens = data.split(";"); 
    this.adminNo = tokens[0]; 
} 

public static String[] readFile(){ 
    String file = "output.txt"; 
    ArrayList <String> studentList = new ArrayList <String>(); 
    try{ 
    FileReader fr = new FileReader(file); 
    Scanner sc = new Scanner(fr); 

    while(sc.hasNextLine()){ 
     studentList.add(new Question3ReadFile(sc.nextLine())); 
    } 

    fr.close(); 
    }catch(FileNotFoundException exception){ 
     System.out.println("File " + file + " was not found"); 
    }catch(IOException exception){ 
     System.out.println(exception); 
    } 
    return studentList.toArray(new String[studentList.size()]); 
} 

回答

2

hasNext和next代替hasNextLine和nextLine

public static void main(String[] args) { 
    String input = " For example, 134567H;Gabriel;24/12/1994;67;78;89"; 
    Scanner scanner = new Scanner(input); 
    scanner.useDelimiter(";"); 
    String firstPart = null; 
    while(scanner.hasNext()){ 
     firstPart = scanner.next(); 
     break; 
    } 

    String secondPart = input.split(firstPart)[1].substring(1); 
    System.out.println(firstPart); 
    System.out.println(secondPart); 
    scanner.close(); 
} 
+0

但這只是打出了從文本文件中的整行到6個選項。我正在嘗試像我選擇管理員號碼那樣,然後所有來自該行所選管理員號碼的信息都將顯示在文本框中。 – Rauryn

+0

@Rauryn我什麼都不懂。你能重新說它嗎? – Eugene

+0

對不起,我的英語。所以,我選擇了一個管理員號碼,然後剩下的信息會顯示在其他地方。根據你的代碼,它只是簡單地將整行分成幾個選項。 – Rauryn

1

不要在這種情況下使用的分隔符。我建議將一個Student對象排除在外。

studentList.add(new Student(sc.nextLine)); 

,並有學生類:

public class Student { 
    private final String adminNo; 

    public Student(String data) { 
     String[] tokens = data.split(";"); 
     this.adminNo = tokens[0]; 
    } 


    public String getAdminNo() { 
     return adminNo; 
    } 
} 

,然後你剛纔讀你以後需要的字段(student.getAdminNo())爲例。

這種方法更漂亮,更容易在以後擴展。

UPD:簡單的方法

還是不要愚蠢OO打擾,只是這樣做:

studentList.add(sc.nextLine.split(";")[0]); 
+0

「新學生」來自哪裏?因爲那裏的「添加」有錯誤。你能向我解釋一下嗎?這是一個類名嗎? – Rauryn

+0

修改答案。現在包括超級真棒學生類 – Rince

+0

我修改了我的帖子,你能幫我檢查我的代碼有什麼問題嗎?我在那裏添加錯誤。 – Rauryn