0
Given a text file as described below:
MARIA;5;MPOO
MARIA;3;AED
MARIA;5;MPOO
JOHN;1;IP
How to create a logic to obtain a result (which groups by equal names and discipline and adds the interactions), such as:
MARIA;10;MPOO
MARIA;3;AED
JOHN;1;IP
I managed to create, as in the code below, just to show everything in separate lines, but I’m not able to put it together. This code takes the student’s name, the number of times he interacted with a subject and what the subject.
public class ArquivoInteracao {
//ESSA PASTA NO C: SÓ É CRIADA SE EU FOR LÁ MANUALMENTE E CRIÁ-LA, EXISTE ALGUMA FORMA DELA
//SER CRIADA AUTOMATICAMENTE?
static java.io.File diretorio = new java.io.File("C:\\Venera");
boolean statusDir = diretorio.mkdir();
public static File arquivo = new File(diretorio, "exibirInteracao.txt");
public ArquivoInteracao() {
try {
boolean statusArq = arquivo.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
private static StringBuilder mensagem;
public static List<String> Read(String Caminho) {
List<String> conteudo = new ArrayList<>();
try {
// Files.readAllLines existe a partir do Java 1.7 em diante
List<String> linhas = Files.readAllLines(arquivo.toPath());
for (String linha : linhas) {
conteudo.add(linha);
}
} catch (IOException ex) {
System.out.println("Erro: Não foi possível ler o arquivo!");
} catch (Exception ex) {
System.out.println("Erro: Arquivo não encontrado!");
}
return conteudo;
}
public static boolean Write(String Texto) {
try {
if (!arquivoExiste()) {
arquivo.createNewFile();
System.out.println("not");
}
System.out.println("Yes");
FileWriter arq = new FileWriter(arquivo, true);
PrintWriter gravarArq = new PrintWriter(arq);
gravarArq.println(Texto);
gravarArq.close();
return true;
} catch (IOException e) {
System.out.println(e.getMessage());
return false;
}
}
public static void salvar(AlunoCliente ac) {
String print = ControleTelaInicial.nomeAlunoParaInteracao + ";" + ControllerPainel.contInteração + ";" +
ControleTelaInicial.comboAlunoParaInteracao;
if (ArquivoInteracao.Write(print)) {
System.out.println("Arquivo salvo com sucesso!");
} else {
System.out.println("Erro ao salvar o arquivo!");
}
}
public static void mostrar(){
try {
List<String> linhas = ArquivoInteracao.Read("exibirInteracao.txt");
mensagem = new StringBuilder();
if (linhas != null && !linhas.isEmpty()) {
for (int i = 0; i < linhas.size(); i++) {
//JÁ TENTEI JUNTAR, MAS NÃO CONSEGUI
String nome = (linhas.get(i).split(";")[0]);
int qtdeInteracao = Integer.parseInt(linhas.get(i).split(";")[1]);
String disciplina = (linhas.get(i).split(";")[2]);
mensagem.append(nome+ "você interagiu"+ qtdeInteracao +"vezes na disciplina de"+ disciplina+"\n");
}
}
} catch (IndexOutOfBoundsException e3) {
System.out.println("Nada a exibir: " + e3.getMessage());
}
}
public static boolean arquivoExiste() {
return arquivo.exists();
}
public static StringBuilder getMensagem() {
return mensagem;
}
}
ArrayList
is just a list. It does not miracle. Do not dosplit()
3x to pick up an item in each. Make asplit()
only and store in an array. Then refer to the index. Tip: create a classInteracao
with the single keyNome;Disciplina
and delegate to it the sum of interactions. The classArquivoInteracao
just need to know how to manipulate the files.– Guilherme Brügger
I have to do so because it is for a college project. But you can explain me better, because I could not understand!?
– Manuela carneiro
I can’t get into the logic of how to put these lines together and add up results, it’s very complex
– Manuela carneiro
You don’t have to do it that way unless the problem statement is "Make a Pascal program using the Java language." The algorithm is + or - like this: Open the file; Read the lines; Add the values where the name and discipline are equal; Save the consolidated result in another file. There you have several options to add up. I suggested that you make an Interaction object to make the sum. But if you want to do everything procedural, you can sort the list and then scroll through it by adding each new line to an accumulated value. More than that, only if I do your job for you.
– Guilherme Brügger
This is a very simple question, which uses 3 fundamental concepts for any programmer: 1) Algorithms 2) Data Structures and 3) Object Orientation. If you UNDERSTAND these 3 concepts, will never complain that lack programmer jobs or earn little. Good studies and success!
– Guilherme Brügger
But how is the logic to know if they are equal? the equals of not right
– Manuela carneiro
But you could answer me if there is how to create the folder automatically and not manually?
– Manuela carneiro