Chargement de fichier en java

Fermé
koitra Messages postés 5 Date d'inscription dimanche 10 juin 2007 Statut Membre Dernière intervention 7 août 2008 - 12 juin 2007 à 17:20
koitra Messages postés 5 Date d'inscription dimanche 10 juin 2007 Statut Membre Dernière intervention 7 août 2008 - 23 sept. 2007 à 02:26
bonjour à tous
je dispose de +sieurs fichiers textes de 10 100 et 1000 villes et je voudrais faire un mini menu ou alors un choix pour décider du fichier à charger lors de l'exécution du programme. je ne sais pas comment faire merci de votre aide c'est pour un projet à rendre dans une semaine merci!






import java.io.*;
import java.util.*;

public class Voyageur3
{
//ATTRIBUTS

int nbVille;
String[] listeVilles;
int[][] matriceDistance;
int [] tableauDistance ;
int [] Visites ;
int nbVilleVu;
int [] tabDistance;
int [] ville1;
int [] ville2;
int [] tabTotal;
int tabVille1[][];
int tabVille2[][];
int tabDistancebis[][];
int tabTotalbis[];
int meilleureDistance;
int meilleureVille;
Scanner sc;
String fileName;
RandomAccessFile entree ;
String mode;
//METHODES
//Constructeur

public Voyageur3()
{
chargerFichier("carte10.txt");
}

public void chargerFichier(String fileName)
{
try
{
BufferedReader buf = new BufferedReader(new FileReader(fileName));
String ligne = buf.readLine();
int indice = 0;
if(ligne.startsWith("NbVilles:"))
{


String nb = ligne.substring(9);
nbVille = Integer.parseInt(nb);
listeVilles = new String[nbVille];
matriceDistance = new int[nbVille][nbVille];
tabDistance = new int[nbVille];
ville1 = new int[nbVille];
ville2 = new int[nbVille];
tabTotal = new int[nbVille];
tabVille1 = new int[nbVille][nbVille];
tabVille2 = new int[nbVille][nbVille];
tabDistancebis = new int[nbVille][nbVille];
tabTotalbis = new int[nbVille];
indice = nbVille;
meilleureDistance = 1000000;
meilleureVille = -1;
sc = new Scanner (System.in);

for(int i=0;i<= this.matriceDistance.length-1;i++)
{
for(int j=0;j<= this.matriceDistance.length-1;j++)
{
if(i == j)
{
this.matriceDistance[i][j] = -1;
}
}
}
}
while(indice>0)
{
String ville = buf.readLine();
listeVilles[indice-1] = ville;
indice--;
}
ligne = buf.readLine();
ligne = buf.readLine();
while(ligne != null)
{
StringTokenizer tok = new StringTokenizer(ligne,":");
String ville1 = tok.nextToken();
String ville2 = tok.nextToken();
String distance = tok.nextToken();
int indice1 = rechercheIndiceVille(ville1);
int indice2 = rechercheIndiceVille(ville2);
matriceDistance[indice1][indice2] = Integer.parseInt(distance);
matriceDistance[indice2][indice1] = Integer.parseInt(distance);
ligne = buf.readLine();
}
}
catch(IOException ioe){ioe.printStackTrace();}
}

public int rechercheIndiceVille(String ville)
{
for(int i=0;i<listeVilles.length;i++)
{
if(listeVilles[i].equals(ville))
return i;
}
return -1;
}

public String toString()
{
StringBuffer buf = new StringBuffer();
buf.append("\n");
buf.append("Nombre de Villes: ");
buf.append(nbVille);
buf.append("\n");
buf.append("Liste des villes: \n");
for(int i=0;i<listeVilles.length;i++)
{
buf.append("-");
buf.append("Ville "+i+": "+listeVilles[i]);
buf.append("\n");
}
return buf.toString();
}

public void afficherMatrice()
{
System.out.println("-- Affichage de la matrice: --");
for(int i=0;i<= this.matriceDistance.length-1;i++)
{
System.out.println();
for(int j=0;j<= this.matriceDistance.length-1;j++)
{
System.out.print("Ligne numero: "+i+" Colonne numero: "+j);
System.out.println(" --> "+this.matriceDistance[i][j]+" ");
}
}
System.out.println();
}

public void construireTrajet()
{
int ligne, colonne,cptVille,testVilleExistante,total,touteVille;
touteVille=0;
this.meilleureVille=-1;
this.meilleureDistance=1000000;
while (touteVille<this.matriceDistance.length)
{
ligne=0;
colonne=0;
cptVille=0;
testVilleExistante=0;
total=0;
for(int j=0;j<this.matriceDistance.length;j++)
{
this.tabDistance[j] = 1000000;
}

this.ville1[cptVille] = touteVille;
if (this.matriceDistance[ville1[cptVille]][colonne] == -1)
{
colonne++;
}
this.ville2[cptVille]=colonne;

this.tabDistance[cptVille]=this.matriceDistance[ville1[cptVille]][ville2[cptVille]];
colonne++;
while(ligne < ((this.matriceDistance.length-1)))
{
while(colonne < ((this.matriceDistance.length-1)))
{
while (testVilleExistante<=cptVille)
{
if (colonne == this.ville1[testVilleExistante])
{
colonne++;
testVilleExistante=-1;
}
testVilleExistante++;
}
testVilleExistante=0;

if ((colonne < this.matriceDistance.length) && (this.matriceDistance[ville1[cptVille]][colonne] != -1))
{
if (this.tabDistance[cptVille] > this.matriceDistance[ville1[cptVille]][colonne])
{
this.tabDistance[cptVille] = this.matriceDistance[ville1[cptVille]][colonne];
this.ville2[cptVille] = colonne;
}
}
colonne++;
}
colonne = 0;
ville1[cptVille+1] = ville2[cptVille];
cptVille++;
ligne++;
}
this.ville2[cptVille]=touteVille;
this.tabDistance[cptVille]=this.matriceDistance[this.ville1[cptVille]][this.ville2[cptVille]];
for(int j=0;j<this.matriceDistance.length;j++)
{
total=total+this.tabDistance[j];
}
this.tabTotal[touteVille]=total;

for(int k=0;k<this.matriceDistance.length;k++)
{
this.tabVille1[touteVille][k]=ville1[k];
this.tabVille2[touteVille][k]=ville2[k];
this.tabDistancebis[touteVille][k]=this.tabDistance[k];
this.tabTotalbis[touteVille]=total;
}
touteVille++;
}
for(int j=0;j<this.matriceDistance.length;j++)
{
if (this.meilleureDistance > this.tabTotal[j])
{
this.meilleureDistance=this.tabTotal[j];
this.meilleureVille=j;
}
}
System.out.println();
}

public void afficherTrajet()
{
System.out.println("-- Affichage des trajets: --");
System.out.println();
for(int j=0;j<this.matriceDistance.length;j++)
{
System.out.println("Affichage du trajet de la ville "+j+":");
for(int k=0;k<this.matriceDistance.length;k++)
{
System.out.println("Ville "+this.tabVille1[j][k]+" a Ville "+this.tabVille2[j][k]+" distante de: "+this.tabDistancebis[j][k]);
}
System.out.println("Distance totale: "+this.tabTotalbis[j]);
System.out.println();
}
}

public void afficherResultat()
{
System.out.println();
System.out.println("Meilleur parcours en partant de la Ville "+this.meilleureVille+" ("+listeVilles[this.meilleureVille]+") avec une distance de "+this.meilleureDistance+"km");
}

public void afficherMenu()
{
int reponse ;
int choix;
do
{
// Affichage des options et appel des sous-programme
// Ex :
System.out.println("--- Probleme du Voyageur De Commerce ---");
System.out.println();
System.out.println("Taper : ");
System.out.println("0 - Afficher toutes les villes");
System.out.println("1 - Afficher la matrice");
System.out.println("2 - Effectuer le calcul du trajet");
System.out.println("3 - Afficher les trajets");
System.out.println("4 - Afficher le meilleur resultat");
// ...
System.out.print("Votre choix : ");
choix = sc.nextInt();
switch (choix)
{
//....
case 0:
Voyageur3 v = new Voyageur3();
System.out.println(v);

break;
case 1:
this.afficherMatrice();
break;
case 2:
this.construireTrajet();
break;
case 3:
this.afficherTrajet();
break;
case 4:
this.afficherResultat();
break;
//...
}
System.out.print("Voulez-vous continuer ? (1 = OUI / 0 = NON) : ");
reponse = sc.nextInt();
System.out.println();
}
while (reponse==1);
}

public static void main(String[] args)
{
Voyageur3 v = new Voyageur3();
v.afficherMenu();
}
}
A voir également:

2 réponses

Doctor C Messages postés 627 Date d'inscription mardi 12 juin 2007 Statut Membre Dernière intervention 19 février 2016 398
12 juin 2007 à 17:48
Si tu veux choisir entre 3 fichiers au début de ton programme et ne plus changer ce fichier jusqu'à ce que tu fermes ton programmes, tu peux toujours faire apparaître une fenêtre dans le main (avant de créer ta classe voyageur3) avec une zone de texte déroulante qui permettrait à l'utilisateur de choisir le fichier désiré. Tu pourrais aussi mettre 3 radio buttons qui auraient comme propriété text le nom de tes fichiers. De cette manière, quand tu appuierais sur Ok (dans la fenêtre de choix), tu cacherais cette fenêtre, et poursuiverais dans ta classe main. Ensuite, dans ta fonction ChargerFichier, tu pourrais vérifier quel bouton est choisi et placer le texte de ce dernier comme paramètre à ta fonction.

J'espère que je suis clair !

Bonne chance
0
koitra Messages postés 5 Date d'inscription dimanche 10 juin 2007 Statut Membre Dernière intervention 7 août 2008
23 sept. 2007 à 02:26
thankssssssssssssssssssssssssss
0