-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadTextFile.java
More file actions
67 lines (57 loc) · 2.34 KB
/
ReadTextFile.java
File metadata and controls
67 lines (57 loc) · 2.34 KB
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
/*
Programmer: Whomever created the project
Additional code by: Dan Hopp, 30-JAN-2020. The additional code will be commented
Description: Read up to 5 numbers from a text file named input.txt saved to the
project root directory. Print the numbers in the order as they appear in
the file.
*/
package lab02;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
public class Hopp_Lab2
{
public static final int SIZE = 5; //try changing to smaller than 5 (what new errors might you see?)
public static void main(String[] args) {
double[] data;
int index;
Scanner input;
data = new double[SIZE];
index = 0;
try {
input = new Scanner(new File("input.txt"));
while (input.hasNext()) {
//Adding a simple logic test. If the file has more items than
//the size of the array, alert the user and exit the loop
if (index == SIZE) {
System.out.println("Alert! The text file can contain no "
+ "greater than " + SIZE + " numbers.");
break;
}
data[index] = input.nextDouble();
System.out.println(data[index]);
index++;
}
}
//Adding a try-catch for an unexpected missing file
catch (FileNotFoundException ex1) {
System.out.println("File not found! If the OS is Windows, is the "
+ "file in the project root folder and named input.txt?");
}
//Adding a try-catch for any non-numeric characters in the file
catch (InputMismatchException ex2) {
System.out.println("Warning! The next character was not a "
+ "number.");
}
//Adding a catch-all as a backup, just in case for any unforseen
//exceptions
catch (Exception ex3) {
System.out.println("Warning! There was an unforseen exception!\n"
+ ex3.toString() + "\nPlease contact your local dev to fix "
+ " the issue.");
}
//Adding a message so the user knows when the program reached its end
System.out.println("End Program.");
}
}