IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Supprimer une ligne lorsqu'on trouve un caractère


Sujet :

avec Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2014
    Messages : 180
    Points : 64
    Points
    64
    Par défaut Supprimer une ligne lorsqu'on trouve un caractère
    Hello tout le monde,
    Alors j'aimerai supprimer toute les lignes de mon fichier contenant un "$".

    Je m'explique j'ai mon fichier :
    #!/bin/csh -xv
    #BSUB -J calmod_POGO_2CU fichie.txt
    #BSUB -o calmod_POGO_2CU.o%J
    #BSUB -L /bin/csh

    umask 002

    #----------------------------------------------------------------------
    set dir_listing = jdfgjkdfnbjkdfnbjnfdbndfjbndfbndfkb
    jnfjkg
    $gdfgdfg
    dfgdfgdfg74654654fdg
    fdgdfg
    fgdf
    set toto = tata
    fgfg
    fg;hmfh
    set toto = tutu2
    dfdsfgdfg
    set nono = ueueue777
    set khjkhjkhkj 8787687657
    titi = tutu
    set a = titi
    set b = $toto
    set c = 7 + $toto
    set d = $toto + 9
    set e = $nono + $toto
    et mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
     
     
    		public class RecupCleValeurTest {
     
    			public static void main(String[] args) throws FileNotFoundException {
     
    				String fichier = "C:\\Users\\ksmaili\\workspace\\fichiertxtP2CSH.txt";
    				Classement cl = new Classement();
    				// lit le fichier ligne par ligne 
    				try{
    					// lire un fichier
    					InputStream ips=new FileInputStream(fichier);       
    					InputStreamReader ipsr=new InputStreamReader(ips);
    					BufferedReader br=new BufferedReader(ipsr);
    					// lire ligne par ligne 
    					String ligne;
    					while ((ligne=br.readLine())!=null){
    						//System.out.println(ligne);
     
    						if(ligne.matches("^set.*=.*")){
    						    //System.out.println("On récupère : " + ligne);
    						}
    						 Pattern p=Pattern.compile("^set(.*)=(.*)");
    						 Matcher m=p.matcher(ligne);
    						 while(m.find()) {
    							//System.out.println(m.group(1)); // affiche les clés 
    							//System.out.println(m.group(2)); // affiche les valeurs des clés
     
     
    						// Maintenant je vérifie si la clé est déjà présente dans la map
    						boolean cle = cl.verif(m.group(1));
    						boolean cle2 = cl.verif(m.group(2));
     
    						// Si elle n'est pas présente, on l'ajoute dans la map 
    						 if (cle != true && cle2 != true){
    							cl.add(m.group(1), m.group(2));
    							 //System.out.println(cle); 
    						}
     
    						 }
    					}
     
    					cl.afficher();
     
     
     
    					br.close(); // On ferme le flux
    				}		
    				catch (Exception e){
    					System.out.println(e.toString());
    				}
     
     
    		}
    		}
    mon code m'affiche toutes les clé et valeurs des lignes ayant la forme set ... = ... de mon fichier c'est à dire :
    dir_listing = jdfgjkdfnbjkdfnbjnfdbndfjbndfbndfkb
    toto = tata
    a = titi
    d = $toto + 9
    e = $nono + $toto
    nono = ueueue777
    c = 7 + $toto
    b = $toto

    le problème c'est que j'aimerai que lorsque mon programme détecte un "$..." il supprime la ligne en gros il ne me l'affiche pas dans ma console.
    par exemple pour :
    set b = $toto => il me supprime la ligne
    set c = 7 + $toto => il m'affiche c = 7
    set d = $toto + 9 => il m'affiche d = 9
    set e = $nono + $toto => il me supprime la ligne

    Avez vous une petite idée

    Thinks

    ps : je pense utilisé les expressions régulières comme dans mon posts poser récemment

  2. #2
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut,
    il faudra utiliser un

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     !tonString.contains("$")
    eric

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2014
    Messages : 180
    Points : 64
    Points
    64
    Par défaut
    Citation Envoyé par jeffray03 Voir le message
    salut,
    il faudra utiliser un

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     !tonString.contains("$")
    eric
    Salut

    je créer une instance de ma classe DetecteSymboleTest et je fais appel a la methode contains?

    Car c'est ce que j'ai fais mais je ne trouve pas le contains !!

  4. #4
    Membre chevronné
    Avatar de eulbobo
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2003
    Messages
    786
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Novembre 2003
    Messages : 786
    Points : 1 993
    Points
    1 993
    Par défaut
    contains est une méthode de la classe String (entre autres, mais c'est celle là qui nous intéresse ici)
    Je ne suis pas mort, j'ai du travail !

  5. #5
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2014
    Messages : 180
    Points : 64
    Points
    64
    Par défaut
    D'accord mais dans quel but contains va m'aider ?
    en faite il va pas prendre les lignes ou il y a "$"?
    j'ai vu que la méthode contains renvoyer un boolean mais je voit pas en quoi sa m'arrangerai ici ...

  6. #6
    Membre chevronné
    Avatar de eulbobo
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2003
    Messages
    786
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Novembre 2003
    Messages : 786
    Points : 1 993
    Points
    1 993
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    		while ((ligne=br.readLine())!=null){
    			if(!ligne.contains("$")){
    				// la suite du traitement
    Je te donne la solution, je te laisse trouver où la mettre
    Je ne suis pas mort, j'ai du travail !

  7. #7
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2014
    Messages : 180
    Points : 64
    Points
    64
    Par défaut
    contains, c'est pour dire "est-ce que ça contient"...

    Donc unChaineDeCaractère.contains(quelquechose), c'est pour rechercher si tu chaine de caractères contient quelque chose.


    Pour ta problématique à propos de la chaine qui doit supprimer des morceaux, ça commence à devenir touchy
    Si on part de la règle "on prend ce qui commence par $ et ce qui suit et on l'enlève"
    set c = 9 + $toto
    donnerait comme résultat
    set c = 9 +

    Ben oui, le + il va pas disparaître tout seul

    Pareil dans l'autre sens
    set c = $toto + 9
    donnerait comme résultat
    set c = + 9

    Dernier point, tu nous dit que c'est de la forme $toto... Du coup '$' suivi d'une chaîne de caractères? Il peut y avoir des caractères numériques? Des signes bizarres?
    Prenons le problème dans l'autre sens
    Tu veux garder quoi précisément des deux coté de ton signe '=' ?
    d'accord pour contains
    u coup '$' suivi d'une chaîne de caractères? Il peut y avoir des caractères numériques? Des signes bizarres?
    pour l'instant, j'ai que ça comme donnees !

    Alors je résume , en gros pour toutes les lignes de la forme set ... = ... je les recupere.
    ensuite dans mon cas par exemple j'ai recuperer :
    On récupère : set dir_listing = jdfgjkdfnbjkdfnbjnfdbndfjbndfbndfkb
    On récupère : set toto = tata
    On récupère : set toto = tutu2
    On récupère : set nono = ueueue777
    On récupère : set a = titi
    On récupère : set b = $toto
    On récupère : set c = 7 + $toto
    On récupère : set d = $toto + 9
    On récupère : set e = $nono + $toto
    et le but c'est de virer les lignes avec le $...
    mais si une ligne contient un $... + 9 ( ou un autre nombre ) ou 3 ( un nombre ) + $... on vire juste le $...
    au final je devrai avoir : set c = 7 et set d = 9 mais je vire le set e vu qu'il y a le $nono et $toto

  8. #8
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2014
    Messages : 180
    Points : 64
    Points
    64
    Par défaut
    tiens je pense avoir trouver le bon endroit ^^

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
     
     
    		public class RecupCleValeurTest {
     
    			public static void main(String[] args) throws FileNotFoundException {
     
    				String fichier = "C:\\Users\\ksmaili\\workspace\\fichiertxtP2CSH.txt";
    				Classement cl = new Classement();
    				// lit le fichier ligne par ligne 
    				try{
    					// lire un fichier
    					InputStream ips=new FileInputStream(fichier);       
    					InputStreamReader ipsr=new InputStreamReader(ips);
    					BufferedReader br=new BufferedReader(ipsr);
    					// lire ligne par ligne 
    					String ligne;
    					while ((ligne=br.readLine())!=null){
    						//System.out.println(ligne);
     
    						if(ligne.matches("^set.*=.*") ){
    						    //System.out.println("On récupère : " + ligne);
    						}
    						 Pattern p=Pattern.compile("^set(.*)=(.*)");
    						 Matcher m=p.matcher(ligne);
    						 while(m.find()) {
    							//System.out.println(m.group(1)); // affiche les clés 
    							//System.out.println(m.group(2)); // affiche les valeurs des clés
     
     
    						// Maintenant je vérifie si la clé est déjà présente dans la map
    						boolean cle = cl.verif(m.group(1));
    						boolean cle2 = cl.verif(m.group(2));
     
    						// Si elle n'est pas présente, on l'ajoute dans la map 
    						 if (cle != true && cle2 != true && !ligne.contains("$")){
    							cl.add(m.group(1), m.group(2));
    							 //System.out.println(cle); 
    						}
     
    						 }
    					}
     
    					cl.afficher();
     
     
     
    					br.close(); // On ferme le flux
    				}		
    				catch (Exception e){
    					System.out.println(e.toString());
    				}
     
     
    		}
    		}

  9. #9
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2014
    Messages : 180
    Points : 64
    Points
    64
    Par défaut
    Tu veux garder quoi précisément des deux coté de ton signe '=' ?
    Des deux cotés j'ai :
    clé = valeur
    et il y a des lignes avec :
    clé = valeur + $...

    et le $... je veux le supprimer mais il y a des cas ou j'aimerai le supp tout en gardant sa valeur
    exemple :

    set c = 7 + $toto => ici je supp $toto mais je garde : c = 7
    set d = $toto + 9 => ici je supp ùtoto mais je garde d = 9

  10. #10
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    Salut,
    voici un petit exemple vite fait:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    public class DetecteSymboleTest {
     
    	public static void main(String[] args) throws FileNotFoundException {
     
    		String fichier = "source2.txt";
    		DetecteSymboleTest popo = new DetecteSymboleTest();
    		// lit le fichier ligne par ligne
    		try {
    			// lire un fichier
    			InputStream ips = new FileInputStream(fichier);
    			InputStreamReader ipsr = new InputStreamReader(ips);
    			BufferedReader br = new BufferedReader(ipsr);
    			// lire ligne par ligne
    			String ligne;
    			while ((ligne = br.readLine()) != null) {
     
     
    				if (ligne.matches("^set.*=.*")) {
    					if (ligne.contains("$")) {
     
    						ligne = count(ligne);
     
    					}
    					if (ligne.length() > 0)
    						System.out.println("On récupère : " + ligne);
     
    				}
     
    			}
    			br.close(); // On ferme le flux
    		} catch (Exception e) {
    			System.out.println(e.toString());
    		}
     
    	}
     
    	public static String count(String text) {
    		String a = "";
    		String var2 = text;
    		for (int i = 0; i < text.length(); i++)
    			if (text.charAt(i) == '$') {
    				int pos2 = countAt(text, i, ' ');
     
    				if (pos2 > 0) {
    					a = text.substring(i, pos2);
    					if (a.length() > 0) {
    						var2 = var2.replace(a, "");
     
    					}
    				} else {
    					a = text.substring(i, text.length());
    					if (a.length() > 0) {
    						var2 = var2.replace(a, "");
     
    					}
    				}
     
    			}
    		String[] testVar = var2.split("=");
    		if (testVar[1].trim().equals("+"))
    			var2 = "";
    		else {
    			testVar[1] = testVar[1].replace("+", "");
    			var2 = testVar[0] + "= " + (testVar[1].trim());
    		}
     
    		return var2;
    	}
     
    	public static int countAt(String text, int pos, char cara) {
    		for (int i = pos; i < text.length(); i++)
    			if (text.charAt(i) == cara) {
     
    				return i;
    			}
     
    		return -1;
    	}
     
    }
    tu pourras mieux le perfectionner.

    Eric

  11. #11
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2014
    Messages : 180
    Points : 64
    Points
    64
    Par défaut
    Citation Envoyé par jeffray03 Voir le message
    Salut,
    voici un petit exemple vite fait:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    public class DetecteSymboleTest {
     
    	public static void main(String[] args) throws FileNotFoundException {
     
    		String fichier = "source2.txt";
    		DetecteSymboleTest popo = new DetecteSymboleTest();
    		// lit le fichier ligne par ligne
    		try {
    			// lire un fichier
    			InputStream ips = new FileInputStream(fichier);
    			InputStreamReader ipsr = new InputStreamReader(ips);
    			BufferedReader br = new BufferedReader(ipsr);
    			// lire ligne par ligne
    			String ligne;
    			while ((ligne = br.readLine()) != null) {
     
     
    				if (ligne.matches("^set.*=.*")) {
    					if (ligne.contains("$")) {
     
    						ligne = count(ligne);
     
    					}
    					if (ligne.length() > 0)
    						System.out.println("On récupère : " + ligne);
     
    				}
     
    			}
    			br.close(); // On ferme le flux
    		} catch (Exception e) {
    			System.out.println(e.toString());
    		}
     
    	}
     
    	public static String count(String text) {
    		String a = "";
    		String var2 = text;
    		for (int i = 0; i < text.length(); i++)
    			if (text.charAt(i) == '$') {
    				int pos2 = countAt(text, i, ' ');
     
    				if (pos2 > 0) {
    					a = text.substring(i, pos2);
    					if (a.length() > 0) {
    						var2 = var2.replace(a, "");
     
    					}
    				} else {
    					a = text.substring(i, text.length());
    					if (a.length() > 0) {
    						var2 = var2.replace(a, "");
     
    					}
    				}
     
    			}
    		String[] testVar = var2.split("=");
    		if (testVar[1].trim().equals("+"))
    			var2 = "";
    		else {
    			testVar[1] = testVar[1].replace("+", "");
    			var2 = testVar[0] + "= " + (testVar[1].trim());
    		}
     
    		return var2;
    	}
     
    	public static int countAt(String text, int pos, char cara) {
    		for (int i = pos; i < text.length(); i++)
    			if (text.charAt(i) == cara) {
     
    				return i;
    			}
     
    		return -1;
    	}
     
    }
    tu pourras mieux le perfectionner.

    Eric
    Ah ouais pas mal !
    merci Eric mais tu pourraît m'expliquer un peu plus en détail ce que tu as fait stp si sa ne te derrange pas

    thinks

  12. #12
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2014
    Messages : 180
    Points : 64
    Points
    64
    Par défaut
    en output j'ai :
    On récupère : set dir_listing = jdfgjkdfnbjkdfnbjnfdbndfjbndfbndfkb
    On récupère : set toto = tata
    On récupère : set toto = tutu2
    On récupère : set nono = ueueue777
    On récupère : set a = titi
    On récupère : set b =
    On récupère : set c = 7
    On récupère : set d = 9
    je devrai pas avoir :
    set toto = tutu2 car j'ai déjà set toto = tata
    et set b =
    mais je vais y jeter un coup d'oeil

  13. #13
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut,
    une autre ébauche
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    public class DetecteSymboleTest {
    	private static String[] testVar;
    	private static List<String> indexes = new ArrayList<>();
     
    	public static void main(String[] args) throws FileNotFoundException {
     
    		final String fichier = "source2.txt";
    		final DetecteSymboleTest popo = new DetecteSymboleTest();
    		// lit le fichier ligne par ligne
    		try {
    			// lire un fichier
    			final InputStream ips = new FileInputStream(fichier);
    			final InputStreamReader ipsr = new InputStreamReader(ips);
    			final BufferedReader br = new BufferedReader(ipsr);
    			// lire ligne par ligne
    			String ligne;
    			while ((ligne = br.readLine()) != null) {
    				// System.out.println(ligne);
    				testVar = ligne.split("=");
    				if (ligne.matches("^set.*=.*") && !ligne.contains("$")) {
     
    				} else if (ligne.matches("^set.*=.*") && ligne.contains("$")) {
    					ligne = count(ligne);
    				} else {
    					ligne = "";
    				}
    				if (ligne.length() > 0) {
    					if (creerIndex(testVar[0])) {
    						System.out.println("On récupère : " + ligne);
    					}
    				}
     
    			}
    			br.close(); // On ferme le flux
    		} catch (final Exception e) {
    			System.out.println(e.toString());
    		}
     
    		// if(!ligne.contains("$")){
     
    	}
     
    	public static String count(String text) {
    		String a = "";
    		String var2 = text;
    		for (int i = 0; i < text.length(); i++) {
    			if (text.charAt(i) == '$') {
    				final int pos2 = countAt(text, i, ' ');
    				if (pos2 > 0) {
    					a = text.substring(i, pos2);
    					if (a.length() > 0) {
    						var2 = var2.replace(a, "");
    					}
    				} else {
    					a = text.substring(i, text.length());
    					if (a.length() > 0) {
    						var2 = var2.replace(a, "");
    					}
    				}
    			}
    		}
     
    		testVar = var2.split("=");
    		if (testVar[1].trim().equals("+") || testVar[1].trim().isEmpty()) {
    			var2 = "";
    		} else {
    			testVar[1] = testVar[1].replace("+", "");
    			var2 = testVar[0] + "= " + (testVar[1].trim());
     
    		}
    		return var2;
    	}
     
    	public static int countAt(String text, int pos, char cara) {
    		for (int i = pos; i < text.length(); i++) {
    			if (text.charAt(i) == cara) {
    				return i;
    			}
    		}
    		return -1;
    	}
     
    	public static boolean creerIndex(String indexStr) {
    		final String[] indexVar = indexStr.split(" ");
    		boolean isCreated = true;
    		if (indexVar.length > 1) {
    			if (!indexes.contains(indexVar[1])) {
    				indexes.add(indexVar[1]);
    			} else {
    				isCreated = false;
    			}
    		}
     
    		return isCreated;
    	}
    }
    eric

  14. #14
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2014
    Messages : 180
    Points : 64
    Points
    64
    Par défaut
    Citation Envoyé par jeffray03 Voir le message
    salut,
    une autre ébauche
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    public class DetecteSymboleTest {
    	private static String[] testVar;
    	private static List<String> indexes = new ArrayList<>();
     
    	public static void main(String[] args) throws FileNotFoundException {
     
    		final String fichier = "source2.txt";
    		final DetecteSymboleTest popo = new DetecteSymboleTest();
    		// lit le fichier ligne par ligne
    		try {
    			// lire un fichier
    			final InputStream ips = new FileInputStream(fichier);
    			final InputStreamReader ipsr = new InputStreamReader(ips);
    			final BufferedReader br = new BufferedReader(ipsr);
    			// lire ligne par ligne
    			String ligne;
    			while ((ligne = br.readLine()) != null) {
    				// System.out.println(ligne);
    				testVar = ligne.split("=");
    				if (ligne.matches("^set.*=.*") && !ligne.contains("$")) {
     
    				} else if (ligne.matches("^set.*=.*") && ligne.contains("$")) {
    					ligne = count(ligne);
    				} else {
    					ligne = "";
    				}
    				if (ligne.length() > 0) {
    					if (creerIndex(testVar[0])) {
    						System.out.println("On récupère : " + ligne);
    					}
    				}
     
    			}
    			br.close(); // On ferme le flux
    		} catch (final Exception e) {
    			System.out.println(e.toString());
    		}
     
    		// if(!ligne.contains("$")){
     
    	}
     
    	public static String count(String text) {
    		String a = "";
    		String var2 = text;
    		for (int i = 0; i < text.length(); i++) {
    			if (text.charAt(i) == '$') {
    				final int pos2 = countAt(text, i, ' ');
    				if (pos2 > 0) {
    					a = text.substring(i, pos2);
    					if (a.length() > 0) {
    						var2 = var2.replace(a, "");
    					}
    				} else {
    					a = text.substring(i, text.length());
    					if (a.length() > 0) {
    						var2 = var2.replace(a, "");
    					}
    				}
    			}
    		}
     
    		testVar = var2.split("=");
    		if (testVar[1].trim().equals("+") || testVar[1].trim().isEmpty()) {
    			var2 = "";
    		} else {
    			testVar[1] = testVar[1].replace("+", "");
    			var2 = testVar[0] + "= " + (testVar[1].trim());
     
    		}
    		return var2;
    	}
     
    	public static int countAt(String text, int pos, char cara) {
    		for (int i = pos; i < text.length(); i++) {
    			if (text.charAt(i) == cara) {
    				return i;
    			}
    		}
    		return -1;
    	}
     
    	public static boolean creerIndex(String indexStr) {
    		final String[] indexVar = indexStr.split(" ");
    		boolean isCreated = true;
    		if (indexVar.length > 1) {
    			if (!indexes.contains(indexVar[1])) {
    				indexes.add(indexVar[1]);
    			} else {
    				isCreated = false;
    			}
    		}
     
    		return isCreated;
    	}
    }
    eric

    Merci Eric pour l'aide c'es très sympa

    il y a juste quelques partie de code que j'aimerai comprendre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    	while ((ligne = br.readLine()) != null) {
    					// System.out.println(ligne); 
    					testVar = ligne.split("=");
    					if (ligne.matches("^set.*=.*") && !ligne.contains("$")) {
     
    					} else if (ligne.matches("^set.*=.*") && ligne.contains("$")) {
    						ligne = count(ligne);
    					} else {
    						ligne = "";
    					}
    					if (ligne.length() > 0) {
    						if (creerIndex(testVar[0])) {
    							System.out.println("On récupère : " + ligne);
    						}
    					}
     
    				}
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    	public static String count(String text) {
    			String a = "";
    			String var2 = text;
    			for (int i = 0; i < text.length(); i++) {
    				if (text.charAt(i) == '$') {
    					final int pos2 = countAt(text, i, ' ');
    					if (pos2 > 0) {
    						a = text.substring(i, pos2);
    						if (a.length() > 0) {
    							var2 = var2.replace(a, "");
    						}
    					} else {
    						a = text.substring(i, text.length());
    						if (a.length() > 0) {
    							var2 = var2.replace(a, "");
    						}
    					}
    				}
    			}
     
    			testVar = var2.split("=");
    			if (testVar[1].trim().equals("+") || testVar[1].trim().isEmpty()) {
    				var2 = "";
    			} else {
    				testVar[1] = testVar[1].replace("+", "");
    				var2 = testVar[0] + "= " + (testVar[1].trim());
     
    			}
    			return var2;
    		}
     
    		public static int countAt(String text, int pos, char cara) {
    			for (int i = pos; i < text.length(); i++) {
    				if (text.charAt(i) == cara) {
    					return i;
    				}
    			}
    			return -1;
    		}
     
    		public static boolean creerIndex(String indexStr) {
    			final String[] indexVar = indexStr.split(" ");
    			boolean isCreated = true;
    			if (indexVar.length > 1) {
    				if (!indexes.contains(indexVar[1])) {
    					indexes.add(indexVar[1]);
    				} else {
    					isCreated = false;
    				}
    			}
     
    			return isCreated;
    		}
    	}
    Si quelqu'un peut m'expliquer plus en détail pour que je puisse bien comprendre comment Eric a procédé je suis preneur !
    thinks

  15. #15
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    Salut,
    voici quelques explications:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    while ((ligne = br.readLine()) != null) {
    					// je splite pour avoir la clé et la valeur pour une comparaison prochaine
    					testVar = ligne.split("=");
    					if (ligne.matches("^set.*=.*") && !ligne.contains("$")) {
                          // je ne fais rien car la ligne est correcte et correspond par exemple a set blabla = 12
    					} else if (ligne.matches("^set.*=.*") && ligne.contains("$")) {
    					    // Si la ligne est du style set blabla = $asd + 34 alors il me faut supprimer $asd et + 
    						ligne = count(ligne);
    					} else {
    					    // dans le cas contraire exemple eewewewewewewewew  ou esd = wwewe alors je mettre la ligne vide
    						ligne = "";
    					}
    					// Si la ligne n´est pas vide
    					if (ligne.length() > 0) {
    					    // je verifie si son index peut etre creer et sauvegarder dans la liste d´indexes ou pas 
    						if (creerIndex(testVar[0])) {
    							System.out.println("On récupère : " + ligne);
    						}
    					}
     
    				}
     
     
    /**
             * suppimes tout les mots commencant par $ ainsi que les +
             * @param text
             * @return String depourvu de $ et + exemple set a = 8
             */
    	public static String count(String text) {
    		String a = "";
    		String var2 = text;
    		for (int i = 0; i < text.length(); i++)
    			if (text.charAt(i) == '$') {
    				int pos2 = countAt(text, i, ' ');
     
    				if (pos2 > 0) {
    					a = text.substring(i, pos2);
    					if (a.length() > 0) {
    						var2 = var2.replace(a, "");
     
    					}
    				} else {
    					a = text.substring(i, text.length());
    					if (a.length() > 0) {
    						var2 = var2.replace(a, "");
     
    					}
    				}
     
    			}
    		String[] testVar = var2.split("=");
    		if (testVar[1].trim().equals("+"))
    			var2 = "";
    		else {
    			testVar[1] = testVar[1].replace("+", "");
    			var2 = testVar[0] + "= " + (testVar[1].trim());
    		}
     
    		return var2;
    	}
     
    	/**
             * cherches a partir d´un text donné, et a partir d´une position donnée, la prochaine position du caractere a chercher
             * @param text
             * @param pos
             * @param cara
             * @return position du caractere s´il y en a  , -1 s´il ny en a pas.
             */
    	public static int countAt(String text, int pos, char cara) {
    		for (int i = pos; i < text.length(); i++)
    			if (text.charAt(i) == cara) {
     
    				return i;
    			}
     
    		return -1;
    	}
     
     
    	/**
             * verifies si l´index est deja present ou pas dans la list d´indexes,  s´il n´est pas present alors on le sauvegarde
             * @param indexStr
             * @return  si l´index existe deja, alors false sinon true
             */
    	public static boolean creerIndex(String indexStr) {
    		final String[] indexVar = indexStr.split(" ");
    		boolean isCreated = true;
    		if (indexVar.length > 1) {
    			if (!indexes.contains(indexVar[1])) {
    				indexes.add(indexVar[1]);
    			} else {
    				isCreated = false;
    			}
    		}
     
    		return isCreated;
    	}
    si c´est Ok, n´oublies pas de mettre sur resolu.

    Eric

  16. #16
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Mai 2014
    Messages : 180
    Points : 64
    Points
    64
    Par défaut
    Merci Eric

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 5
    Dernier message: 30/04/2015, 11h06
  2. [JTable] Supprimer une ligne d'un jtable
    Par Orionmel dans le forum Composants
    Réponses: 5
    Dernier message: 05/11/2004, 22h29
  3. [C#] Comment supprimer une ligne dans DataGrid ?
    Par BAUDIER dans le forum ASP.NET
    Réponses: 2
    Dernier message: 20/07/2004, 16h03
  4. supprimer une ligne avec cle etrangere
    Par BaBas dans le forum Langage SQL
    Réponses: 4
    Dernier message: 15/07/2003, 11h24
  5. Supprimer une ligne dans un fichier
    Par sbeu dans le forum Langage
    Réponses: 3
    Dernier message: 13/05/2003, 10h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo