Crochet, java

Résolu
maya 22 Messages postés 23 Statut Membre -  
 danimo -
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); 
    } 
}

3 réponses

  1. danimo
     
    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
  2. maya 22 Messages postés 23 Statut Membre
     
    je vous remercie énormément pour votre aide Dan, votre code marche a merveille.

    cordialement,

    maya
    0
  3. danimo
     
    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