Bonjour, je suis un jeune étudiant en 1ère année de DUT Informatique.
J'ai un devoir sur une classe Utilitaire à réaliser mais j'ai un problème dans la réalisation de celle-ci.
Je vous explique, j'ai un tableau à 2 dimension à mettre en forme suivant ce schéma :
Mais lorsque, par exemple j'ai plus de valeur sur une ligne que du nombre que j'ai de ligne, suivant ce schéma :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 +---+---+---+ 0 | R | C | V | +---+---+---+ 1 | W | V | B | +---+---+---+ 2 | X | U | Z | +---+---+---+ 3 | V | U | E | +---+---+---+ 4 | W | B | B | +---+---+---+ 0 1 2
J'obtiens une erreur comme celle-ci à l'exécution :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 +---+---+---+---+---+---+ 0 | R | C | V | A | B | C | +---+---+---+---+---+---+ 1 | W | V | B | A | B | C | +---+---+---+---+---+---+ 2 | X | U | Z | A | B | C | +---+---+---+---+---+---+ 3 | V | U | E | A | B | C | +---+---+---+---+---+---+ 4 | W | B | B | A | B | C | +---+---+---+---+---+---+ 0 1 2 3 4 5
Je ne sais plus du tout comment procéder. Auriez-vous une solution à me donner ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at Tableau.enChaine(Tableau.java:104) at Test.main(Test.java:36)
Voici les lignes de code que j'ai réaliser, dans un premier temps la classe main (nommé Test.java) puis dans un deuxième temps ma classe Utilitaire (nommé Tableau.java)
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 public class Test { public static void main(String[] arg) { /*-----------------*/ /* Variables */ /*-----------------*/ // Variables char t1[][] = {{'R', 'C', 'V', 'A', 'B', 'C', 'D'}, {'W', 'V', 'B', 'A', 'B', 'C', 'D'}, {'X', 'U', 'Z', 'A', 'B', 'C', 'D'}, {'V', 'U', 'E', 'A', 'B', 'C', 'D'}, {'W', 'B', 'B', 'A', 'B', 'C', 'D'}, {'R', 'C', 'V', 'A', 'B', 'C', 'D'}, {'R', 'C', 'V', 'A', 'B', 'C', 'D'}}; /*int t2[][] = {{'1', '2', '3', '4', '5', '6'}, {'1', '2', '3', '4', '5', '6'}, {'1', '2', '3', '4', '5', '6'}, {'1', '2', '3', '4', '5', '6'}, {'1', '2', '3', '4', '5', '6'}, {'1', '2', '3', '4', '5', '6'}, {'1', '2', '3', '4', '5', '6'}};*/ int cpt = 0; char carac; /*-----------------*/ /* Instructions */ /*-----------------*/ System.out.println ( Tableau.enChaine ( t1 ) ); // System.out.println ( Tableau.enChaine ( t2 ) ); } }J'espère profondément que vous pourrez m'apporter une aide.
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 public class Tableau { public static String enChaine( char[] tab ) { String sRet = ""; String lig = "+"; int cpt = 0; while(cpt < (tab.length)) { lig = lig + "---+"; cpt++; } sRet = lig + "\n"; cpt = 0; while(cpt < (tab.length)) { sRet = sRet + "| " + tab[cpt] + " "; cpt++; } cpt = 0; String num = ""; while (cpt < (tab.length)) { num = num + " " + String.format("%3d", cpt); cpt++; } sRet = sRet + "|\n" + lig + "\n" + num + "\n"; return sRet; } public static String enChaine( int[] tab, int largeur ) { String sRet = ""; String lig = "+"; int cpt = 0; while (cpt < (tab.length)) { if ((tab.length) < 10) { lig = lig + "----+"; } else { lig = lig + "--+"; } cpt++; } sRet = lig + "\n"; cpt = 0; while (cpt < (tab.length)) { if ((tab.length) < 10) { sRet = sRet + "|" + String.format("%4d", tab[cpt]); } else { sRet = sRet + "|" + String.format("%2d", tab[cpt]); } cpt++; } cpt = 0; String num = ""; while (cpt < (tab.length)) { if ((tab.length) < 10) { num = num + " " + String.format("%4d", cpt); } else { num = num + " " + String.format("%2d", cpt); } cpt++; } sRet = sRet + "|\n" + lig + "\n" + num + "\n"; return sRet; } public static String enChaine ( char[][] tab ) { String sRet = ""; String lig = " +"; int i; int j = 0; /* i : ligne ; j : colonne */ int cpt = 0; while(cpt < (tab[j].length)) { lig = lig + "---+"; cpt++; } sRet = lig + "\n"; i = 0; while (i < (tab[i].length)) { sRet = sRet + i + " "; j = 0; while (j < (tab[j].length)) { sRet = sRet + " | " + tab[i][j]; j++; } sRet = sRet + " |\n" + lig + "\n"; i++; } sRet = sRet + " "; cpt = 0; while (cpt < (tab[j].length)) { sRet = sRet + " " + cpt; cpt++; } return sRet; } public static String enChaine ( int[][] tab, int largeur ) { String sRet = ""; String lig = " +"; int i; int j = 0; /* i : ligne ; j : colonne */ int cpt = 0; while(cpt < (tab[j].length)) { lig = lig + "---+"; cpt++; } sRet = lig + "\n"; i = 0; while (i < (tab[i].length)) { sRet = sRet + i + " "; j = 0; while (j < (tab[j].length)) { sRet = sRet + " | " + tab[i][j]; j++; } sRet = sRet + " |\n" + lig + "\n"; i++; } sRet = sRet + " "; cpt = 0; while (cpt < (tab[j].length)) { sRet = sRet + " " + cpt; cpt++; } return sRet; } }
Cordialement, Daethe.
Partager