這個代碼好像是壞了,但我不知道爲什麼:簡單string.split()了可怕的錯誤
System.out.println(line);
// prints: Some Name;1|IN03PLF;IN02SDI;IN03MAP;IN02SDA;IN01ARC
String args[] = line.split("|");
String candidatArgs[] = args[0].split(";");
String inscrieriString[] = args[1].split(";");
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(candidatArgs);
System.out.println("[0]:" + candidatArgs[0]);
System.out.println("[1]:" + candidatArgs[1]);
// prints: S
// [Ljava.lang.String;@4f77e2b6
// [0]:
我不知道爲什麼發生這種情況。通過我的邏輯:
String args[] = line.split("|");
[0]: Some Name;1
[1]: IN02SDI;IN03MAP;IN02SDA;IN01ARC
相反的:
[0]: S
如果您想了解更多的代碼:即使它並沒有做太多這應該編譯(去掉儘可能多的非必要代碼,因爲我可以)
主營:
有一個文件:Candidati.txt
含有:Some Name;1|IN03PLF;IN02SDI;IN03MAP;IN02SDA;IN01ARC
import java.util.ArrayList;
Repository repository = new Repository ("Candidati.txt"); // file name
ArrayList<Candidat> candidati = repository.getCandidati();
System.out.println(candidati);
庫
import java.util.ArrayList;
public class Repository {
private String fisierCuCandidati;
private ArrayList<Candidat> listaCandidati;
public Repository (String fisierCuCandidati) {
this.fisierCuCandidati = fisierCuCandidati; // file name
this.listaCandidati = new ArrayList<Candidat>();
this.incarcaCandidati();
}
public void incarcaCandidati() {
FileReader in = null;
BufferedReader input = null;
//try {
in = new FileReader (this.fisierCuCandidati);
input = new BufferedReader (in);
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
String args[] = line.split("|");
String candidatArgs[] = args[0].split(";");
String inscrieriString[] = args[1].split(";");
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(candidatArgs);
System.out.println("[0]:" + candidatArgs[0]);
System.out.println("[1]:" + candidatArgs[1]);
}
}
Candidat
public class Candidat {
public Candidat (String nume) {
}
public Candidat (String nume, int id) {
}