/Users/teofilomatos/Documents/isep/APROG_2010_2011/projectos/PL8_EX_DEM/src/pl8_ex_dem/Main.java
 1 package pl8_ex_dem;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.util.Formatter;
 6 import java.util.Scanner;
 7 
 8 /**
 9  * @author APROG 2009/2010 - 1ND
10  */
11 public class Main {
12 
13     private static int lerFicheiro(int[] vNumero, String[] vNome) throws FileNotFoundException {
14         String[] arr;
15         String s;
16         int i = 0;
17         Scanner input = new Scanner(new File("Alunos2.txt"));
18 
19         // enquanto tiver dados para ler
20         while (input.hasNext()) {
21             s = input.nextLine();
22             arr = s.split("/"); // divide informação pelo '/' e coloca num vector de Strings
23             vNumero[i] = Integer.parseInt(arr[0]);
24             vNome[i] = arr[1].trim();
25             i++;
26         }
27         
28         // fecha o ficheiro
29         if (input != null) {
30             input.close();
31         }
32 
33         return i;
34     }
35 
36     // ordena a informação por ordem crescente de número de alunos
37     private static void ordenar(int[] vNum, String[] vN, int nelem) {
38         String auxS;
39         int auxI;
40 
41         for (int i = 0; i < nelem - 1; i++) {
42             for (int j = i + 1; j < nelem; j++) {
43                 if (vNum[i] > vNum[j]) {
44                     // troca o nome
45                     auxS = vN[i];
46                     vN[i] = vN[j];
47                     vN[j] = auxS;
48                     // troca o número
49                     auxI = vNum[i];
50                     vNum[i] = vNum[j];
51                     vNum[j] = auxI;
52                 }
53             }
54         }
55     }
56 
57     // escreve o vector de strings para um ficheiro
58     private static void escreverFicheiro(String[] str) throws FileNotFoundException {
59         int nelem = str.length;
60         Formatter output = new Formatter(new File("AlunosOrdenado.txt"));
61 
62         for (int i = 0; i < nelem; i++) {
63             output.format("%s%n", str[i]);
64         }
65 
66         if (output != null) {
67             output.close();
68         }
69     }
70 
71     /*
72      * Considere que existe um ficheiro texto de nome “Alunos.txt”, com informação
73      * sobre os alunos inscritos numa dada instituição. Cada linha do ficheiro tem
74      * a seguinte formatação: "Número / Nome" do aluno
75      * Pretende-se criar um outro ficheiro de texto com as linhas formatadas de
76      * igual modo, mas ordenado por número do aluno
77      *
78      */
79     public static void main(String[] args) throws FileNotFoundException {
80         String[] vNome = new String[20];
81         int[] vNumero = new int[20];
82         int nelem;
83 
84         // le do ficheiro
85         nelem = lerFicheiro(vNumero, vNome);
86         ordenar(vNumero, vNome, nelem);
87 
88         // constrói um vector de strings
89         String[] str = new String[nelem];
90         for (int i = 0; i < nelem; i++) {
91             str[i] = vNumero[i] + "/" + vNome[i];
92         }
93 
94         // escrever no ficheiro
95         escreverFicheiro(str);
96     }
97 }
98 
99