Reputation: 71
I'm trying to read and transfer each line of a .txt file into a vector format. It seems like my information are being converted since vectPers.size()
give me 21 which is the amount of people i have.
The problem is that when i try to print the vector it keeps displaying these wierd lines which account for 21 people
Personne@15db9742
Personne@6d06d69c
Personne@7852e922
Personne@4e25154f
Personne@70dea4e
Personne@5c647e05
Personne@33909752
Personne@55f96302
Personne@3d4eac69
Personne@42a57993
Personne@75b84c92
Personne@6bc7c054
Personne@232204a1
Personne@4aa298b7
Personne@7d4991ad
Personne@28d93b30
Personne@1b6d3586
Personne@4554617c
Personne@74a14482
Personne@1540e19d
Personne@677327b6
This is my code so far:
import java.io.*;
import java.util.Vector;
class Personne
{
private String nomPre ;
private double taille, poids;
private char sexe ;
private int numero;
public Personne( String nomPre, char sexe, double taille, double poids, int numero) {
this.nomPre = nomPre ;
this.sexe = sexe;
this.taille = taille ;
this.poids = poids;
this.numero = numero;
}
public double getTaille() { return taille; }
public double getPoids() {return poids; }
public String getNomPre() { return nomPre; }
public char getSexe() { return sexe ; }
public int getNumero() { return numero; }
public void afficher()
{
System.out.printf("%5d %30s %6.2f %7.1f %s\n",
numero, nomPre, taille, poids, sexe == 'F' ? "feminin":"masculin");
}
}
public class numero3
{
static int lireAfficher(String nomFichier)
throws IOException
{
final int LONG_NP = 30, COL1 = 37, COL2 = 41,
COL3 = 51, COL4 = 56,
COL5 = 64, COL6 = 68;
int n = 0;
boolean existeFichier = true ;
FileReader fr = null;
try {
fr = new FileReader (nomFichier) ;
}
catch ( java.io.FileNotFoundException erreur) {
System.out.println("Probleme d'ouvrir le fichier " +
nomFichier);
existeFichier = false ;
}
if (existeFichier) {
BufferedReader entree = new BufferedReader(fr);
boolean finFichier = false ;
while ( !finFichier ) {
String uneLigne = entree.readLine();
if (uneLigne == null)
finFichier = true ;
else {
String unNom = uneLigne.substring(0,LONG_NP);
char unSexe = uneLigne.charAt(LONG_NP);
double uneTaille =
( new Double(uneLigne.substring(COL1, COL2).trim())).doubleValue();
double unPoids =
( new Double(uneLigne.substring(COL3, COL4).trim())).doubleValue();
int unNumero = Integer.parseInt(uneLigne.substring(COL5, COL6).trim());
n++;
Personne unePers = new Personne(unNom, unSexe, uneTaille, unPoids, unNumero);
unePers.afficher();
}
}
entree.close();
}
return n;
}
static int lireRemplir(String nomFichier,Personne[]pers,Vector<Personne>vectPers)
throws IOException
{
final int LONG_NP = 30, COL1 = 37, COL2 = 41,
COL3 = 51, COL4 = 56,
COL5 = 64, COL6 = 68;
int n = 0;
boolean existeFichier = true ;
FileReader fr = null;
try {
fr = new FileReader (nomFichier) ;
}
catch ( java.io.FileNotFoundException erreur) {
System.out.println("Probleme d'ouvrir le fichier " +
nomFichier);
existeFichier = false ;
}
if (existeFichier) {
BufferedReader entree = new BufferedReader(fr);
boolean finFichier = false ;
while ( !finFichier ) {
String uneLigne = entree.readLine();
if (uneLigne == null)
finFichier = true ;
else {
String unNom = uneLigne.substring(0,LONG_NP);
char unSexe = uneLigne.charAt(LONG_NP);
double uneTaille =
( new Double(uneLigne.substring(COL1, COL2).trim())).doubleValue();
double unPoids =
( new Double(uneLigne.substring(COL3, COL4).trim())).doubleValue();
int unNumero = Integer.parseInt(uneLigne.substring(COL5, COL6).trim());
pers[n++]= new Personne(unNom, unSexe, uneTaille, unPoids, unNumero);
vectPers.add(new Personne(unNom, unSexe,uneTaille,unPoids,unNumero));
}
}
entree.close();
}
return n;
}
public static void main (String[] args)
throws IOException
{
int nbPers = lireAfficher("met_a14.txt");
System.out.printf("On vient de lire %d personnes\n", nbPers);
Vector<Personne> vectPers = new Vector<Personne> ();
final int MAX_PERS=nbPers;
Personne[] pers=new Personne[MAX_PERS];
int nbPerso = lireRemplir("met_a14.txt",pers,vectPers);
for(int i=0;i<vectPers.size();i++){
System.out.println(vectPers.get(i));
}
}
I was wondering if it is because i need to create a class Vector like my class Personne. But cant find any information on how to start it off.
Upvotes: 0
Views: 683
Reputation: 24124
You should override the toString()
method in Person
class to get a meaningful output. The value printed is from the default implementation of toString()
in java.lang.Object
class.
This page on toString()
in JavaPractices should give you more information.
Upvotes: 1