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

Collection et Stream Java Discussion :

Valider un 2-dimensionnel Array


Sujet :

Collection et Stream Java

  1. #1
    Futur Membre du Club
    Inscrit en
    Juin 2009
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Juin 2009
    Messages : 3
    Points : 5
    Points
    5
    Par défaut Valider un 2-dimensionnel Array
    public static int[][] inputStringToStateArray(String input)
    je suis debutant en java et veux pouvoir dans cette methode vérifier si le string est correct
    la première dimension doit etre les lignes, la seconde les colones et le reste les données de la matrix qui doivent etre de meme longueur, Exemple:
    5-6-100000-010101-100010-000100-000000
    quelques idées, solutions??

    merci et bonne journée

    Watle

  2. #2
    Rédacteur
    Avatar de CyberChouan
    Homme Profil pro
    Directeur technique
    Inscrit en
    Janvier 2007
    Messages
    2 752
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur technique
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Janvier 2007
    Messages : 2 752
    Points : 4 314
    Points
    4 314
    Par défaut
    Citation Envoyé par watle Voir le message
    quelques idées, solutions??
    La méthode String.split() pour découper ta chaîne. Ensuite, une analyse morceau par morceau.
    Avant de poster, pensez à regarder la FAQ, les tutoriaux, la Javadoc (de la JRE que vous utilisez) et à faire une recherche
    Je ne réponds pas aux questions techniques par MP: les forums sont faits pour ça
    Mes articles et tutoriaux & Mon blog informatique

  3. #3
    Membre expérimenté Avatar de herve91
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    1 282
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 282
    Points : 1 608
    Points
    1 608
    Par défaut
    Une proposition de code, à adapter si besoin :
    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
      public final static int DATA_LENGTH = 6;
     
      static class Transition {
        int row;
        int col;
        int[] datas;
     
        public Transition(int row, int col, int[] datas) {
          this.row = row;
          this.col = col;
          this.datas = datas;
        }
      }
     
      public static int[][][] inputStringToStateArray(String input) {
     
        int rowMax = 0;
        int colMax = 0;
        List<Transition> listeTransitions = new ArrayList<Transition>();
     
        for (String transition : getTransitions(input)) {
          String[] parts = transition.split("-");
          if (parts.length < 3) {
            throw new IllegalArgumentException("format incorrect : '" + transition
                + "' doit être de la forme n-m-data1-data2-...");
          }
     
          int row = toInt(parts[0]);
          if (row > rowMax) {
            rowMax = row;
          }
          int col = toInt(parts[1]);
          if (col > colMax) {
            colMax = col;
          }
     
          int[] datas = new int[parts.length - 2];
          for (int i = 0; i < datas.length; i++) {
            String s = parts[i + 2];
            if (s.length() != DATA_LENGTH) {
              throw new IllegalArgumentException("donnée incorrecte : '" + s
                  + "' doit être de longueur " + DATA_LENGTH);
            }
            datas[i] = toInt(s);
          }
          listeTransitions.add(new Transition(row, col, datas));
        }
     
        int stateArray[][][] = new int[rowMax + 1][colMax + 1][];
        for (Transition transition : listeTransitions) {
          stateArray[transition.row][transition.col] = transition.datas;
        }
        return stateArray;
      }
     
      private static String[] getTransitions(String input) {
        // à définir
      }
     
      private static int toInt(String s) {
        try {
          return Integer.parseInt(s);
        } catch (NumberFormatException e) {
          throw new IllegalArgumentException("type entier attendu : '" + s + "'");
        }
      }

  4. #4
    Futur Membre du Club
    Inscrit en
    Juin 2009
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Juin 2009
    Messages : 3
    Points : 5
    Points
    5
    Par défaut
    Citation Envoyé par CyberChouan Voir le message
    La méthode String.split() pour découper ta chaîne. Ensuite, une analyse morceau par morceau.
    Oui, mais si ta chaine contient x ligne et y colones?? une analyse morceau par morceau serait pas alors possible..

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

Discussions similaires

  1. [XL-2010] Tri Array bi-dimensionnel
    Par SuperKiwi dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 23/04/2014, 12h40
  2. [ZF 1.10] Validators multiples via array
    Par jfdio dans le forum Zend_Form
    Réponses: 3
    Dernier message: 02/06/2010, 11h07
  3. Array multi dimensionnelle et associative
    Par nico33307 dans le forum Langage
    Réponses: 6
    Dernier message: 09/05/2008, 07h05
  4. [Conception] [array] probleme de validation
    Par ozzmax dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 04/10/2006, 17h03
  5. [VB6] Evenement validate
    Par grosjej dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 05/09/2002, 15h46

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