-
Notifications
You must be signed in to change notification settings - Fork 24
#35 Exception Handling - Manipulação de Arquivos CSV #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
d1ebb73
864abb1
e2399ba
14ecdc4
2ed930d
1cbbea7
0694a0b
2cbeddd
05992c9
f7c88cb
215806c
1d43381
f7479b1
a1131b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import java.io.BufferedReader; | ||
| import java.io.FileReader; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| public class ListaPessoas { | ||
|
|
||
| public static List<Pessoa> lerCsvEConverteParaPessoas(String arquivoCSV) { | ||
| List<Pessoa> pessoasList = null; | ||
| try (BufferedReader br = new BufferedReader(new FileReader(arquivoCSV))) { | ||
| pessoasList = new ArrayList<>(); | ||
| String linha; | ||
| boolean primeiraLinha = true; | ||
| while ((linha = br.readLine()) != null) { | ||
| if (primeiraLinha) { | ||
| primeiraLinha = false; | ||
| continue; // Ignora o cabeçalho | ||
| } | ||
| String[] campos = linha.split(","); | ||
| if (campos.length == 3) { | ||
| String nome = campos[0].trim(); | ||
| int idade = Integer.parseInt(campos[1].trim()); | ||
| String email = campos[2].trim(); | ||
| Pessoa pessoa = new Pessoa(nome, idade, email); | ||
| pessoasList.add(pessoa); | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| System.out.println("Erro ao ler o arquivo: " + e.getMessage()); | ||
| } | ||
| return pessoasList; | ||
| } | ||
|
|
||
| public static double calcularMediaIdade(List<Pessoa> pessoaList){ | ||
| return pessoas.stream() | ||
| .mapToDouble(Pessoa::getIdade) | ||
| .average() | ||
| .orElse(0.0); | ||
| } | ||
|
|
||
|
Comment on lines
+35
to
+41
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aqui não está compilando porquê pessoas não existe, o nome do parametro é pessoaList |
||
| public static void main(String[] args) { | ||
| String pathArquivoCsv = "Exception Handling/ManipulacaoCSV/com.manipulacao-csv.java/dados.csv"; | ||
| List<Pessoa> pessoas = PreencheLista(pathArquivoCsv); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mesma coisa aqui, como mudou o nome do metodo, o PreencheLista não existe mais |
||
| if(!pessoas.isEmpty()) { | ||
| pessoas.forEach(System.out::println); | ||
| System.out.println("Média de idade das pessoas: "+ calcularMediaIdade(pessoas)); | ||
| } else { | ||
| System.out.println("Arquivo está vazio"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
|
|
||
| public class Pessoa{ | ||
| private String nome; | ||
| private int idade; | ||
| private String email; | ||
|
|
||
| public Pessoa(String nome, int idade, String email) { | ||
| this.nome = nome; | ||
| this.idade = idade; | ||
| this.email = email; | ||
| } | ||
|
|
||
| public String getNome() { | ||
| return nome; | ||
| } | ||
|
|
||
| public int getIdade() { | ||
| return idade; | ||
| } | ||
|
|
||
| public String getEmail() { | ||
| return email; | ||
| } | ||
|
|
||
| @Override | ||
| public java.lang.String toString() { | ||
| return "Pessoa{" + | ||
| "nome='" + nome + '\'' + | ||
| ", idade=" + idade + | ||
| ", email='" + email + '\'' + | ||
| '}'; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| Pessoa pessoa1 = new Pessoa("Fran", 28, "fran@email.com"); | ||
|
|
||
| System.out.println(pessoa1); | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| Nome,Idade,Email | ||
| Ada Lovelace,36,ada@email.com | ||
| Grace Hopper,55,grace@email.com | ||
| Hedy Lamarr,65,hedy@email.com | ||
| Katherine Johnson,101,kath@email.com | ||
| Radia Perlman,70,radia@email.com | ||
| Shafi Goldwasser,68,shafi@email.com | ||
| Adele Goldberg,74,adele@@email.com | ||
| Rear Admiral Grace Murray Hopper,85, rear@email.com | ||
| Margaret Hamilton,85,margaret@email.com | ||
| Susan Kare,65,susan@email.com |
Uh oh!
There was an error while loading. Please reload this page.