1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| package files;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class files {
/**
* Constructeur
*/
public files() {
int[] tableau = null;
try (BufferedReader b = new BufferedReader(new FileReader(new File("d:/taxe.txt")))) {
String line;
// Lecture du fichier ligne par ligne. Cette boucle se termine
// quand la méthode retourne la valeur null.
while ((line = b.readLine()) != null) {
String[] args = line.split("\\s+");
int[] lecture = new int[args.length - 1]; // on crée un tableau
// temporaire pour
// la lecture
for (int i = 1; i < args.length; i++) {
lecture[i - 1] = Integer.parseInt(args[i]);
}
if (tableau == null) {
tableau = lecture;
} else { // on va créer un tableau temporaire pour copier les 2
// autres dedans
int[] temp = new int[tableau.length + lecture.length];
System.arraycopy(tableau, 0, temp, 0, tableau.length); // copie tableau dans temp
System.arraycopy(lecture, 0, temp, tableau.length, lecture.length); // copie lecture dans temp (même remarque que ligne précédente)
tableau = temp;
}
}
} catch (IOException e) {
e.printStackTrace();
}
// affichage
System.out.println("Valeurs dans tableau : ");
for (int i = 0; i < tableau.length; i++) {
System.out.print(tableau[i]);
if (((i + 1) % 5) == 0) {
System.out.println();
} else {
System.out.print(" ");
}
}
int indiceTableau = 0;
int[] tableauIndiceCherche = new int[tableau.length];
int[] tableauIndiceTrouve = new int[tableau.length];
for (int i = 0; i < tableau.length - 1; i++) {
int indiceTrouve = recherche(tableau[i], tableau, i + 1);
if (indiceTrouve >= 0) {
// par exemple
tableauIndiceCherche[indiceTableau] = i;
tableauIndiceTrouve[indiceTableau] = indiceTrouve;
indiceTableau++; // on passe à la case suivante
}
}
// afichage des résultats
for(int i=0; i<indiceTableau; i++) {
System.out.println("La valeur tableau[" + tableauIndiceCherche[i]+"]="+tableau[tableauIndiceCherche[i]]+" a été trouvée en double en position " + tableauIndiceTrouve[i] + ": tableau["+tableauIndiceTrouve[i]+"]="+tableau[tableauIndiceTrouve[i]]);
}
}
public static int recherche(int maValeur, int[] tab, int depart) {
int monIndice = -1;
for (int i = depart; monIndice == -1 && i < tab.length; i++) {
if (maValeur == tab[i]) {
// System.out.println(i);
monIndice = i;
}
}
return monIndice;
}
/**
* MAIN
*/
public static void main(String[] args) {
// On lance notre lire fichier
new files();
}
} |
Partager