problème avec class vector
bonjour je vient de creer une programme java dont j'avais besoin d'utiliser la class util.vector;
le problème vient au niveau de parcours de la liste qui me donne juste le dernier enregistrement saisie repeté selon la taille du table vector du total elements saisie
voici le programme :
Code:
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 107 108 109 110 111 112
| import java.util.*;
import java.io.*;
//import java.nio.*;
class Personne{
///////////////
static String nom;
static String prenom;
static int age;
//////////////
static Scanner s = new Scanner(System.in);
//////////////
public static Vector<Personne> v = new Vector<Personne>();
/////////////////////
public Personne(String n, String p,int a){
nom = n;
prenom = p;
age = a;
}
////////////////////
public static void Identite(){
System.out.println(nom+" "+prenom+" have "+age+" years old");
}
///////////////////////
public static void Instance(){
System.out.println("Put Your Last Name :");
nom = s.next();
System.out.println("Put Your Fisrt Name :");
prenom = s.next();
System.out.println("Puts Your Age :");
age = s.nextInt();
Personne p = new Personne(nom,prenom,age);
v.addElement(p);
}
/////////////////////
public static void ScreenPrint(){
//Personne.Instance();
//Personne s;
for(int i=0 ; i<v.size();i++){
v.elementAt(i).Identite();
}
}
//////////////////////
public static void DeleteItem(int pl){
v.remove(pl);
}
//////////////////////
public static void InFile()
{
//File out = new File("print.txt" );
String destination = "print.txt";
//PrintWriter fichier = new PrintWriter(new FileWriter(destination));
for(int i=0 ; i<v.size();i++){
try{
/*FileWriter fw = new FileWriter(out);
PrintWriter pw = new PrintWriter(fw);*/
String s="";
PrintWriter fichier = new PrintWriter(new FileWriter(destination));
s += nom+" "+prenom+" have "+age+" years old";
fichier.println(s+"--Register at :"+new Date()+"--\n\r\n");
fichier.close();
}catch(IOException e){
System.out.println(e.getMessage());
}
}
}
/////////////////////////////
public static void ExitScreen(){
//String c;
System.out.println("Exit !!");
System.exit(1);
}
///////////////////////
public static void Tool(){
System.out.println("SELECT A NUMBER TO BRWOSE THE LIST : ");
System.out.println("-1- Add a person");
System.out.println("-2- Delete a person");
System.out.println("-3- Print the list of persons");
System.out.println("-4- Save your Work in a file");
System.out.println("-5- EXIT");
int x = 6;
while(x != 0)
{
x = s.nextInt();
switch(x)
{
case 1 :
Instance();
break;
case 2 :
System.out.println("Put the index of personne :");
int indice = s.nextInt();
DeleteItem(indice);
break;
case 3 :
ScreenPrint();
break;
case 4 :
InFile();
break;
case 5 :
ExitScreen();
break;
default :
System.out.println("The number is not in the list ! Try agin");
}
}
}
////////////////////
} |
donc si je fait '1' : ça va me permettre de saisir et avec 3 j'affiche , alors si
si je fait aaa-bbb-25 et après ccccc-dddd-55
lorsque je saisi '3' pour afficher la liste ça donne comme resultat :
ccc ddd have 55 years old
ccc ddd have 55 years old
alors si quelqu'un peut me dire pourquoi svp ?
merci pour votre aide;)