toledo9
Messages postés22Date d'inscriptionmardi 25 janvier 2011StatutMembreDernière intervention31 mars 2022 27 janv. 2012 à 18:58
J'ai des problèmes pour exécuter mon programme en Java.
Comment faire ?
Si possible je voudrais rajouter une fonction supprimer le contact.
Total : 5 Codes
Merci pour l'aide,
1er code : Pas de probleme me semble t-il ?
import java.io.*;
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException,ClassNotFoundException{
int choix;
GlobalPersonne c = new GlobalPersonne();
Fichier f=new Fichier();
if(f.ouvrir("lecture"))
{
c=f.lire();
f.fermer();
}
do
{
System.out.println("\n");
choix=menu();
switch(choix)
{
case 1 : c.ajouterPersonne();
break;
case 2 : c.rechercherPersonne();
break;
case 3 : c.afficherListe();
break;
case 4 : f.ouvrir("ecriture");
f.ecrire(c);
f.fermer();
System.out.println("fichier sauvegarder!");
break;
case 5 : System.exit(0);
break;
}
}while(choix!=5);
}
/**menu vers les differentes partie du programme*/
public static int menu()
{
int choix;
System.out.println("MENU PRINCIPAL");
System.out.println("--------------");
System.out.println("1.Ajouter une personne");
System.out.println("2.Rechercher une personne");
System.out.println("3.Afficher la liste");
System.out.println("4.Sauvegarder");
System.out.println("5.Sortir");
System.out.println("--------------");
System.out.print("votre choix : ");
choix=Lire.i();
return choix;
}
}
2eme code : Fichier d'écriture /
import java.io.*;
public class Fichier {
/** Creates a new instance of Fichier */
private ObjectOutputStream fW;
private ObjectInputStream fR;
private String nomDeFichier="Fichier.dat";
private char mode;
public Fichier() {
}
/**
* ouverture du fichier:
* fait par les constructeurs
* FileInputStream etObjectInputStream * ecriture du fichier
* fait par les constructeurs
* FileOutputStreametObjectOutputStream */
public boolean ouvrir(String s)throws IOException
{
try
{
mode =(s.toUpperCase()).charAt(0);
if(mode=='R'||mode=='L')
fR=new ObjectInputStream(new FileInputStream(nomDeFichier));
else if(mode=='W'||mode=='E')
fW=new ObjectOutputStream(new FileOutputStream(nomDeFichier));
return true;
}
catch( IOException e)
{
return false;
}
}
public void ecrire(GlobalPersonne tmp) throws IOException
{
if(tmp!=null)fW.writeObject(tmp);
}
public GlobalPersonne lire()throws IOException,ClassNotFoundException
{
GlobalPersonne tmp=( GlobalPersonne)fR.readObject();
return tmp;
}
public void fermer()throws IOException
{
if(mode=='R'||mode=='L')
fR.close();
else if(mode=='W'||mode=='E')
fW.close();
}
}
3eme code :
import java.util.*;
import java.io.*;
public class GlobalPersonne implements Serializable {
private Vector listePersonne;
public GlobalPersonne() {
listePersonne=new Vector();
}
public void ajouterPersonne()
{
listePersonne.addElement(new Personne());
}
public void rechercherPersonne()
{
String verifNom;
String verifPrenom;
String catchNom;
String catchPrenom;
int size;
int i;
int present=0;
Personne temp;
System.out.println("entrer le nom : ");
verifNom=Lire.S();
System.out.println("entrer le prenom : ");
verifPrenom=Lire.S();
size=listePersonne.size();
/*la boucle suivante me permet de verifier le nom et le prenom de la*/
/*personne rechercher*/
for(i=0;i<size;i++)
{
temp=(Personne)listePersonne.elementAt(i);//cast
catchNom=temp.envoyerNom();//recupere le nom de la classe personne
catchPrenom=temp.envoyerPrenom();//recupere le prenom de la classe personne
if((verifNom.equals(catchNom))&&(verifPrenom.equals(catchPrenom)))
{
present++;
//incremente si nom et prenom egale
temp.afficherPersonne();
}
}
if(present==0)
System.out.println("La personne n'est pas presente dans la liste !");
}
public void afficherListe()
{
int i;
int size;
Personne temp;
size=listePersonne.size();
if(size==0)
System.out.println("Liste vide !");
else
{
System.out.print("\n");
[b]FAIRE une RECHERCHE sur RS -
résultat donne( le nom prenom et mail de la personne) /b
4eme code : Fonction lire
import java.io.*;
public class Lire
{
public static String S() // Lire un String
{
String tmp = "";
char C='\0';
try {
while ((C=(char) System.in.read()) !='\n')
{
if (C !'\r') tmp tmp+C;
}
}
catch (IOException e)
{
System.out.println("Erreur de frappe");
System.exit(0);
}
return tmp;
} // fin de S()
public static byte b() // Lire un entier byte
{
byte x=0;
try {
x=Byte.parseByte(S());
}
catch (NumberFormatException e) {
System.out.println("Format numérique incorrect");
System.exit(0);
}
return x ;
}
public static short s() // Lire un entier short
{
short x=0;
try {
x=Short.parseShort(S());
}
catch (NumberFormatException e) {
System.out.println("Format numérique incorrect");
System.exit(0);
}
return x ;
}
public static int i() // Lire un entier
{
int x=0;
try {
x=Integer.parseInt(S());
}
catch (NumberFormatException e) {
System.out.println("Format numérique incorrect");
System.exit(0);
}
return x ;
}
public static long l() // Lire un entier long
{
long x=0;
try {
x=Integer.parseInt(S());
}
catch (NumberFormatException e) {
System.out.println("Format numérique incorrect");
System.exit(0);
}
return x ;
}
public static double d() // Lire un double
{
double x=0.0;
try {
x=Double.valueOf(S()).doubleValue();
}
catch (NumberFormatException e) {
System.out.println("Format numérique incorrect");
System.exit(0);
}
return x ;
}
public static float f() // Lire un float
{
float x=0.0f;
try {
x=Double.valueOf(S()).floatValue();
}
catch (NumberFormatException e)
{
System.out.println("Format numérique incorrect");
System.exit(0);
}
return x ;
}
public static char c() // Lire un caractere
{
String tmp=S();
if (tmp.length()==0)
return '\n';
else
{
return tmp.charAt(0);
}
}
}
5eme code
mport java.io.*;
public class Personne implements Serializable{
private String rs, nom, prenom, mail;
/**le constructeur me permet d'initialiser mes variables definnissant
*ma classe personne*/
public Personne() {
System.out.println("RS : ");
rs=Lire.S();
System.out.println("Nom : ");
nom=Lire.S();
System.out.println("Prenom : ");
prenom=Lire.S();
System.out.println("Mail : ");
mail=Lire.S();
}
public void afficherPersonne()
{
System.out.println(rs+"/"+nom+"/"+prenom+"/"+mail);
}
public String envoyerNom()
{
return nom;
}
public String envoyerPrenom()
{
return prenom;
}
}
[b]Problème n'affiche pas RS, Nom, Prenom, Mail
J'aimerais que le fichier.txt soit plus lisible au final/b