Advertisement
vinedfs

Leitor de Notas

Jul 22nd, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. package br.com.geracao.main;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6.  
  7. import javax.swing.JOptionPane;
  8.  
  9. public class LeitorDeNotas {
  10.  
  11.     public static void main(String[] args) {
  12.        
  13.         // Cria um FileReader (Abre um arquivo e deixa pronto para leitura).
  14.         FileReader fileReader = null;
  15.         try {
  16.             fileReader = new FileReader("C:\\arquivo.txt");
  17.         } catch (FileNotFoundException e) {
  18.             JOptionPane.showMessageDialog(null, "O arquivo não foi encontrado.");
  19.             return;
  20.         }
  21.        
  22.         int alunosComMaisDe7 = 0;
  23.         float total = 0f;
  24.         String linha;
  25.        
  26.         // Lê o arquivo no fileReader usando o BufferedReader.
  27.         BufferedReader br = new BufferedReader(fileReader);
  28.         try {
  29.             linha = br.readLine();
  30.             while (linha != null) {
  31.                  float nota = Float.parseFloat(linha);
  32.                  total += nota;
  33.                 if (nota > 7) {
  34.                     alunosComMaisDe7 ++;
  35.                 }
  36.                 linha = br.readLine();
  37.             }
  38.             br.close();
  39.         } catch (Exception e) {
  40.             JOptionPane.showMessageDialog(null, "Erro ao ler o arquivo (" + e.getLocalizedMessage() + ").");
  41.         }
  42.         float porcentagem = (alunosComMaisDe7 * 100/19);
  43.        
  44.         System.out.println("Porcentagem com mais de 7: " + porcentagem + "%");
  45.         System.out.println("Porcentagem com menos de 7: " + (100 - porcentagem) + "%");
  46.         System.out.println("Média da turma: " + (total/19));
  47.  
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement