質問編集履歴

1

現在のソースコード

2017/05/21 21:40

投稿

ProgramP
ProgramP

スコア6

test CHANGED
File without changes
test CHANGED
@@ -16,4 +16,270 @@
16
16
 
17
17
  正常ルートやlongの64ビット整数値以上ではnull、文字列の場合nullはできています。
18
18
 
19
- 65536個以上、非CSV形式、nullを入力した場合、処理結果をnullとする方法がわかりません。その際のソースコードを教えていただけないでしょうか?
19
+ 65536個以上、非CSV形式、nullを入力した場合、処理結果をnullとする方法がわかりません。その際のソースコードを教えていただけないでしょうか?```Java
20
+
21
+
22
+
23
+ ``````MainActivity.java
24
+
25
+ public class MainActivity extends AppCompatActivity {
26
+
27
+
28
+
29
+ @Override
30
+
31
+ protected void onCreate(Bundle savedInstanceState) {
32
+
33
+ super.onCreate(savedInstanceState);
34
+
35
+ setContentView(R.layout.activity_main);
36
+
37
+
38
+
39
+ String longNum = new String();
40
+
41
+
42
+
43
+ for (int count = 0; count < 0; count++) {
44
+
45
+ if (count == 0) {
46
+
47
+ longNum = 0 + "";
48
+
49
+ } else {
50
+
51
+ longNum = longNum + "," + 0;
52
+
53
+ }
54
+
55
+ }
56
+
57
+ String conv = StringConverter.sortCsvFormatString(longNum);
58
+
59
+ }
60
+
61
+ }
62
+
63
+ ```
64
+
65
+ ```StringConverter.java
66
+
67
+ public class StringConverter {
68
+
69
+
70
+
71
+ // 検索文字の定数作成
72
+
73
+ private static final String SEARCH_WORD = ",";
74
+
75
+ // 配列の定数作成
76
+
77
+ private static final int MAX_COUNT = 65535;
78
+
79
+
80
+
81
+ public static String sortCsvFormatString(String string) {
82
+
83
+ String convertStr = null;
84
+
85
+
86
+
87
+ // long型の配列で65535個
88
+
89
+
90
+
91
+ long tempLong[] = new long[MAX_COUNT];
92
+
93
+
94
+
95
+
96
+
97
+ // カンマの位置を検索する。
98
+
99
+ // カンマの位置までの文字列を取得する。
100
+
101
+ // long型で配列に格納する。
102
+
103
+ // 検索したところから文字列を削除する。
104
+
105
+
106
+
107
+ int startPos = 0;
108
+
109
+ int count = 0;
110
+
111
+
112
+
113
+ // // CSV形式以外の時、null
114
+
115
+ // try {
116
+
117
+ // tempLong[count] = Long.parseLong(tempString);
118
+
119
+ // } catch (java.lang.NumberFormatException e){
120
+
121
+ // return null;
122
+
123
+ // }
124
+
125
+ // if ("," がないとき){
126
+
127
+ // nullで返す
128
+
129
+ //}
130
+
131
+
132
+
133
+ // // 65536個以上の時、null
134
+
135
+ // try {
136
+
137
+ // tempLong[count] = Long.parseLong(tempString);
138
+
139
+ // } catch (java.lang.NumberFormatException e){
140
+
141
+ // return null;
142
+
143
+ // }
144
+
145
+ // if (tempLong.length >= 65536) {
146
+
147
+ // return null;
148
+
149
+ // }
150
+
151
+
152
+
153
+ // // nullの時、null
154
+
155
+ // try {
156
+
157
+ // tempLong[count] = Long.parseLong(tempString);
158
+
159
+ // } catch (java.lang.NumberFormatException e){
160
+
161
+ // return null;
162
+
163
+ // }
164
+
165
+ //if ( = null){
166
+
167
+ // return null;
168
+
169
+ //}
170
+
171
+
172
+
173
+ // くり返し文 検索が終わった場合もしくは65535回回した場合終了
174
+
175
+ for (; count < MAX_COUNT; count++) {
176
+
177
+ int cursorPos = string.indexOf(SEARCH_WORD, startPos);
178
+
179
+
180
+
181
+ if (cursorPos == -1) {
182
+
183
+ String tempString = string.substring(startPos, string.length());
184
+
185
+ try {
186
+
187
+ tempLong[count] = Long.parseLong(tempString);
188
+
189
+ } catch (java.lang.NumberFormatException e) {
190
+
191
+ return null;
192
+
193
+ }
194
+
195
+
196
+
197
+ tempLong[count] = Long.parseLong(tempString);
198
+
199
+ count++;
200
+
201
+ break;
202
+
203
+ }
204
+
205
+
206
+
207
+ String tempString = string.substring(startPos, cursorPos);
208
+
209
+ startPos = cursorPos + SEARCH_WORD.length();
210
+
211
+ tempLong[count] = Long.parseLong(tempString);
212
+
213
+ }
214
+
215
+
216
+
217
+ long tempSort[] = new long[count];
218
+
219
+ for (count = 0; count < tempSort.length; count++) {
220
+
221
+ tempSort[count] = tempLong[count];
222
+
223
+ }
224
+
225
+
226
+
227
+ // 最後の要素を除いて、すべての要素を並べ替えます
228
+
229
+ for (count = 0; count < tempSort.length - 1; count++) {
230
+
231
+
232
+
233
+ // 下から上に順番に比較します
234
+
235
+ for (int arrayOrder = tempSort.length - 1; arrayOrder > count; arrayOrder--) {
236
+
237
+
238
+
239
+ // 上の方が大きいときは互いに入れ替えます
240
+
241
+ if (tempSort[arrayOrder] < tempSort[arrayOrder - 1]) {
242
+
243
+ long arrayChange = tempSort[arrayOrder];
244
+
245
+ tempSort[arrayOrder] = tempSort[arrayOrder - 1];
246
+
247
+ tempSort[arrayOrder - 1] = arrayChange;
248
+
249
+ }
250
+
251
+ }
252
+
253
+ }
254
+
255
+ count = 0;
256
+
257
+ for (; count < tempSort.length; count++) {
258
+
259
+ if (count == 0) {
260
+
261
+ convertStr = String.valueOf(tempSort[count]);
262
+
263
+ } else {
264
+
265
+ convertStr = convertStr + SEARCH_WORD + String.valueOf(tempSort[count]);
266
+
267
+ }
268
+
269
+ }
270
+
271
+
272
+
273
+ if (conv.contains(",")) {
274
+
275
+ } return null;
276
+
277
+
278
+
279
+ return convertStr;
280
+
281
+ }
282
+
283
+ }
284
+
285
+ ```