Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.geracao.main;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import javax.swing.JOptionPane;
- public class LeitorDeNotas {
- public static void main(String[] args) {
- // Cria um FileReader (Abre um arquivo e deixa pronto para leitura).
- FileReader fileReader = null;
- try {
- fileReader = new FileReader("C:\\arquivo.txt");
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(null, "O arquivo não foi encontrado.");
- return;
- }
- int alunosComMaisDe7 = 0;
- float total = 0f;
- String linha;
- // Lê o arquivo no fileReader usando o BufferedReader.
- BufferedReader br = new BufferedReader(fileReader);
- try {
- linha = br.readLine();
- while (linha != null) {
- float nota = Float.parseFloat(linha);
- total += nota;
- if (nota > 7) {
- alunosComMaisDe7 ++;
- }
- linha = br.readLine();
- }
- br.close();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Erro ao ler o arquivo (" + e.getLocalizedMessage() + ").");
- }
- float porcentagem = (alunosComMaisDe7 * 100/19);
- System.out.println("Porcentagem com mais de 7: " + porcentagem + "%");
- System.out.println("Porcentagem com menos de 7: " + (100 - porcentagem) + "%");
- System.out.println("Média da turma: " + (total/19));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement