/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
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
20 while (input.hasNext()) {
21 s = input.nextLine();
22 arr = s.split("/");
23 vNumero[i] = Integer.parseInt(arr[0]);
24 vNome[i] = arr[1].trim();
25 i++;
26 }
27
28
29 if (input != null) {
30 input.close();
31 }
32
33 return i;
34 }
35
36
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
45 auxS = vN[i];
46 vN[i] = vN[j];
47 vN[j] = auxS;
48
49 auxI = vNum[i];
50 vNum[i] = vNum[j];
51 vNum[j] = auxI;
52 }
53 }
54 }
55 }
56
57
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
73
74
75
76
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
85 nelem = lerFicheiro(vNumero, vNome);
86 ordenar(vNumero, vNome, nelem);
87
88
89 String[] str = new String[nelem];
90 for (int i = 0; i < nelem; i++) {
91 str[i] = vNumero[i] + "/" + vNome[i];
92 }
93
94
95 escreverFicheiro(str);
96 }
97 }
98
99