質問編集履歴
1
IOExeptionを入れた後のコードとエラーです。
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,6 +16,8 @@
|
|
16
16
|
|
17
17
|
例外java.io.FileNotFoundExceptionは報告されません。スローするには、捕捉または宣言する必要があります
|
18
18
|
|
19
|
+
>>fileInputのシンボルが見つけられません
|
20
|
+
|
19
21
|
```
|
20
22
|
|
21
23
|
|
@@ -158,6 +160,156 @@
|
|
158
160
|
|
159
161
|
}
|
160
162
|
|
163
|
+
------------------------------------
|
164
|
+
|
165
|
+
import java.util.Scanner;
|
166
|
+
|
167
|
+
import java.io.*;
|
168
|
+
|
169
|
+
public class Lab4_3 {
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
public static void main (String[] args){
|
174
|
+
|
175
|
+
//declair the valiables
|
176
|
+
|
177
|
+
int numberInput;
|
178
|
+
|
179
|
+
int integerInput;
|
180
|
+
|
181
|
+
int digitSum = 0;
|
182
|
+
|
183
|
+
int counter = 0;
|
184
|
+
|
185
|
+
String fileName = "";
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
//get an input
|
190
|
+
|
191
|
+
Scanner input = new Scanner(System.in);
|
192
|
+
|
193
|
+
System.out.println("How many numbers in input");
|
194
|
+
|
195
|
+
numberInput = input.nextInt();
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
//see if the input is positive or negative
|
200
|
+
|
201
|
+
while(numberInput - 1 >= counter){
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
try{
|
206
|
+
|
207
|
+
File openFile = new File(fileName);
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
// it checks if the file exists
|
212
|
+
|
213
|
+
if (openFile.exists()) {
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
// it will then crate a scanner object for the file
|
218
|
+
|
219
|
+
Scanner fileInput = new Scanner(openFile);
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
catch (IOException e) {
|
228
|
+
|
229
|
+
System.out.println("IOExceptionが発生");
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
// it then will check if there is no integers in the file
|
236
|
+
|
237
|
+
if (!fileInput.hasNextInt()) {
|
238
|
+
|
239
|
+
// it will give an error if there is no error
|
240
|
+
|
241
|
+
System.out.println("The file doesn't contain any integers. Exit program!");
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
else {
|
248
|
+
|
249
|
+
// it will then loop through the integers until there is no more integers
|
250
|
+
|
251
|
+
// anymore
|
252
|
+
|
253
|
+
while (fileInput.hasNextInt()) {
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
// it will get the next integer from the file
|
258
|
+
|
259
|
+
int lineInput = fileInput.nextInt();
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
if (lineInput <= 0){
|
264
|
+
|
265
|
+
System.out.println("It is a negative integer.");
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
//if positive, stat a loop and get the sum of the gidits
|
272
|
+
|
273
|
+
else{
|
274
|
+
|
275
|
+
while (lineInput != 0){
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
digitSum += lineInput % 10;
|
282
|
+
|
283
|
+
lineInput = lineInput / 10;
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
System.out.println("The sum of the digits is " + digitSum);
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
counter++;
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
}
|
312
|
+
|
161
313
|
```
|
162
314
|
|
163
315
|
|
@@ -168,6 +320,10 @@
|
|
168
320
|
|
169
321
|
IOExceptionを入れようとしましたが、書き方だ正しくないのか、さらにエラーが出てしましました。
|
170
322
|
|
323
|
+
>>追記としてIOExeptionを入れたものです。
|
324
|
+
|
325
|
+
fileInputのシンボルが見つけられません というエラーが出ています。
|
326
|
+
|
171
327
|
|
172
328
|
|
173
329
|
### 補足情報(FW/ツールのバージョンなど)
|