gpt4 book ai didi

java - 一个简单的 string.split() 出了可怕的错误

转载 作者:行者123 更新时间:2023-11-29 09:41:21 25 4
gpt4 key购买 nike

<分区>

这段代码似乎有问题,但我不知道为什么:

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]);
}


}

候选人

public class Candidat {

public Candidat (String nume) {

}

public Candidat (String nume, int id) {

}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com