Rechercher : dans
Par :

Chargement de fichier en java

Dernière réponse le 23 sep 2007 à 02:26:37 koitra, le 12 jun 2007 à 17:20:25 
 Signaler ce message aux modérateurs

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();
}
}

Configuration: Windows XP
Internet Explorer 6.0

Meilleures réponses pour « chargement de fichier en java » dans :
MySQL - Chargement d'un fichier texte dans une table VoirPour charger une fichier texte défini comme suit : $ tail /home/user1/test.txt 'nom1',1,9 'nom2',2,3 'nom3',3,54 'nom4',4,2 'nom5',5,9 Dans une table définie comme suit : CREATE TABLE chargertest ( ...
Comment lire un fichier ligne par ligne VoirComment lire un fichier ligne par ligne Préambule Boucle while Syntaxe Exemple Astuces Bonus Boucle for Syntaxe Préambule Une des erreurs les plus communes dans l'apprentissage des scripts "bash" sous GNU/LInux pour lire un fichier...
GRUB - La structure de fichier de configuration VoirLe fichier de configuration de GRUB Introduction Exemple de configuration type Explications Paramètres par défaut 1. Adressage des disques 2. title 3. root 4. kernel 5. initrd 6. rootnoverify 7. chainloader OBSERVATIONS
Java - Premier programme VoirPremière application avec Java La première chose à faire est de créer un simple fichier texte (sans mise en forme) et de taper les quelques lignes suivantes : // Votre premiere application en Java class FirstApp { public static void main...
Formats et extensions de fichiers VoirRemarque : La liste ci-dessous n'est pas exhaustive et certains noms d'extension peuvent avoir plusieures fonctionnalités. Quant aux programmes proposés pour utiliser ces fichiers, ils ne sont pas forcément les seuls, ni les plus...
J2EE - Java 2 Enterprise Edition VoirIntroduction au Java Framework Le «Java Framework» (Java 2 Platform) est composé de trois éditions, destinées à des usages différents : J2ME : Java 2 Micro Edition est prévu pour le développement d'applications embarquées, notamment sur des...

1

Doctor C, le 12 jun 2007 à 17:48: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 Echo "Lima Mike Alfa";

Répondre à Doctor C

2

 koitra, le 23 sep 2007 à 02:26:37

Thankssssssssssssssssssssssssss

Répondre à koitra