Crochet, java

Résolu/Fermé
maya 22 Messages postés 15 Date d'inscription samedi 26 février 2011 Statut Membre Dernière intervention 23 mai 2011 - Modifié par maya 22 le 16/04/2011 à 15:46
 Utilisateur anonyme - 17 avril 2011 à 19:26
Bonjour,
j'essaye de faire un programme qui a pour but d'insérer des crochets dans un chaîne de caractère du type "pomme((carotte(radis)), chocolat(caramel))" je souhaite insérer un crochet ouvrant après la dernière parenthèse ouvrante et un crochet fermant avant la première parenthèse fermante, et cela pour chaque sous chaîne (avant la virgule) ce qui donnerai
"pomme((carotte([radis])), chocolat([caramel]))"

Pour cela j'ai écris ce petit programme en java, mais cela ne marche pas, aidez moi a trouver le problème merci

public class Main { 

public static void main(String[] args) { 
        // TODO code application logic here 
        String input=  "pomme((carotte(radis)), chocolat(caramel))"; 
        String[] sentences = input.split(","); 
        //String[] sent; 

   for (int i = 0; i < sentences.length; i++) { 
    int l=0; 
    char[] sentenceTab = sentences[i].toCharArray(); 
                                int leng=sentences.length+4; 
                                //String[] sent=sentences[leng]; 
                                char[] sentenceTab1 = sentences[leng].toCharArray(); 

    char c1=' '; 

               int k; 
    for(k =0; k < sentenceTab.length ; k++){ 
                                    for (l=0; l<sentenceTab1.length; l++) 
                                      { sentenceTab1[l]=sentenceTab[k]; 
                                    }} 
                                for (l=0; l<sentenceTab1.length+2; l++) 
                                {   c1=sentenceTab[l]; 
                                        if (c1==')'){ 
                                            int v = l ; 
                                            for (l=sentenceTab1.length+2; l>0; l--) 
                                            {   sentenceTab1[l]=sentenceTab1[l-1]; 
                                                } 
                                            sentenceTab1[v]=']'; 

                                } 
                                } 
                                for (l=sentenceTab1.length+2; l>0; l--) 
                                {char c = sentenceTab[l-1]; 
                                 if (c== '('){ 
                                     int j=c; 
                                     for (int v=sentenceTab1.length+2; v>0; v--) 
                                     { sentenceTab1[v]=sentenceTab1[v-1]; 
                                     } 
                                     sentenceTab1[j+1]='['; 
                                 } 
                                 
                                 } 
                                    sentences[i] = String.copyValueOf(sentenceTab1); 

                                } 
                        input = ""; 
   for (int i = 0; i < sentences.length-1; i++) { 
    input += sentences[i]+","; 
                        } 
                         
                        input+=sentences[sentences.length-1]; 
                        System.out.println(input); 
    } 
}
A voir également:

3 réponses

Bonjour,

On peut faire un peu plus simple:

       public class Crochets 
   { 
      private static String inputNew = ""; 
       
      public static void main(String[] args) 
      { 
         String input= "pomme((carotte(radis)), chocolat(caramel))"; 
         // Pour obtenir "pomme((carotte([radis])), chocolat([caramel]))" : 
       
         String[] sentences = input.split(","); 
         for(int i = 0; i <sentences.length; i++) 
         { 
            String str = sentences[i]; 
            //System.out.println(">>>> str" + "\"" + sentences[i] + "\";"); 
            int index1 = str.lastIndexOf("("); 
            int index2 = str.indexOf(")"); 
            if( (index1 > -1) || (index2 > -1) ) 
            { 
               inputNew+= str.substring(0, index1 +1) + ("[") 
                     + str.substring (index1 +1, index2) 
                     + ("]") + str.substring(index2) + (","); 
               //System.out.println("inputNew = " + "\"" + inputNew + "\";"); 
            } 
         } 
         inputNew = inputNew.substring(0,inputNew.length() -1); 
         System.out.println("inputNew = " + "\"" + inputNew + "\";"); 
      } 
   }


Cordialement,

Dan

Ps,
Je vais essayer de voir pourquoi ton code ne donne pas le résultat escompté...
0
maya 22 Messages postés 15 Date d'inscription samedi 26 février 2011 Statut Membre Dernière intervention 23 mai 2011
16 avril 2011 à 22:25
je vous remercie énormément pour votre aide Dan, votre code marche a merveille.

cordialement,

maya
0
Re,

Avec cette remarque importante:

Il faudrait ajouter un contrôle du format de la chaîne input.

D'autre part j'ai oublié ceci:

      
            if( (index1 > -1) || (index2 > -1) ) 
            { 
               inputNew+= str.substring(0, index1 +1) + ("[") 
                     + str.substring (index1 +1, index2) 
                     + ("]") + str.substring(index2) + (","); 
               //System.out.println("inputNew = " + "\"" + inputNew + "\";"); 
            } 
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Ajouter : 
            else 
            { 
               System.out.println("Erreur de format !!!"); 
               return; 
            } 
         } 
......
..........


Cordialement,

Dan
0