[RESOLVIDO] Quebrar a String em Pedaços e Inserir em uma determinada Classe

4 respostas
splitprogramaçãojava
R

Pessoal,
Sou iniciante no Java e tenho a seguinte dúvida:
Como faço para ler, delimitar os dados da String e inserir na Classe Corresp ?

for(int i=0; i<posicoes.size();i++) {

String correspondencias = posicoes.get(i);

System.out.println(correspondencias);

}

o código abaixo apresenta uma String que me retorna os seguintes dados:

31,Acceptance-Active_conference_participant,0.68,0.54,0.14,0.05,0,0.22,0.18
31,Co-author-Co-chair,0.82,0.6,0.38,0,0,0,0.19
31,ExternalReviewer-Extended_abstract,0.77,0.41,0.22,0.05,0,0.18,0.2
11,PaperFullVersion-Conference_announcement,0,0.32,0,0.11,0.51,0.75,0.215
31,Administrator-Abstract,0.76,0.42,0.24,0.07,0,0.2,0.22
11,Decision-Conference_proceedings,0,0.43,0,0.1,0.35,0.67,0.225
31,Co-author-Contribution_1th-author,0.63,0.62,0.5,0,0,0,0.25
31,Preference-Presentation,0.75,0.36,0.23,0.06,0.08,0.35,0.29
11,SubjectArea-Track,0.43,0.25,0,0.12,0.42,0.71,0.335
11,Rejection-Accepted_contribution,0.6,0.4,0.24,0.08,0.29,0.75,0.345
31,Acceptance-Accepted_contribution,0.9,0.43,0.34,0.06,0.18,0.5,0.385

Criei uma Classe Corresp:

public class Corresp {

private int clu;
private  String nome;
private double m1;
private double m2;
private double m3;
private double m4;
private double m5;
private double m6;


public Corresp(int clu, String nome, double m1, double m2, double m3, double m4, double m5, double m6) {
	this.clu = clu;
	this.nome = nome;
	this.m1 = m1;
	this.m2 = m2;
	this.m3 = m3;
	this.m4 = m4;
	this.m5 = m5;
	this.m6 = m6;
}


public int getClu() {
	return clu;
}


public String getNome() {
	return nome;
}


public double getM1() {
	return m1;
}


public double getM2() {
	return m2;
}


public double getM3() {
	return m3;
}


public double getM4() {
	return m4;
}


public double getM5() {
	return m5;
}


public double getM6() {
	return m6;
}
public String toString() {
	return clus+ ","+ nome + "," + m1 + "," + m2 + "," + m3 + "," + m4 + "," + m5 + "," + m6;

}

}

4 Respostas

J

Você pode usar o método split da classe String, ela recebe um delimitador como parametro e retorna um array de string delimitadas pelo argumento. Ficaria assim:

for(int i=0; i<posicoes.size();i++) {

String correspondencias = posicoes.get(i);

String[] strDividida =  correspondencias.split(",");

String clu = strDividida[0];

String nome = strDividida[1]; e assim por diante

}
R

Mas preciso inserir no formato int clus, String nome, double m1,…

R

Obrigado. Funcionou!

J

Basta fazer o parse

Criado 7 de julho de 2018
Ultima resposta 7 de jul. de 2018
Respostas 4
Participantes 2