You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Much of this class is copied and adjusted from previous PowerPoints
2
+
3
+
// This imports the StringTokenizer class for use in this class
4
+
importjava.util.StringTokenizer;
5
+
6
+
// This creates a new public class called Project1
7
+
publicclassProject1
8
+
{
9
+
// This creates a new main method
10
+
publicstaticvoidmain(String[] args)
11
+
{
12
+
// This creates a new TextFileInput using the first command-line argument as the file name
13
+
TextFileInputtfi = newTextFileInput(args[0]);
14
+
// This initializes an arbitrarily large String array to store the dates
15
+
String[] dates = newString[1000];
16
+
// This uses the readLine method from TextFileInput to store the first String in the file, which is null if the file is empty
17
+
Stringline = tfi.readLine();
18
+
// This creates a new unitialized StringTokenizer
19
+
StringTokenizerst;
20
+
// This sets the initial value for the index of the array to 0
21
+
intindex = 0;
22
+
// This creates an uninitialized String to store each day temporarily before testing and either accepting and storing or rejecting and printing them
23
+
Stringday;
24
+
// This performs the actions within as long as the line does not equal null (file has more lines) and index of the array is less than the length of the arbitraily long String array
25
+
while(line != null && index < dates.length)
26
+
{
27
+
// Each time the loop is performed, this creates a initializes the StringTokenizer with the line from the File and "," as parameters
28
+
st = newStringTokenizer(line, ",");
29
+
// This performs the actions within as long as the StringTokenizer hasMoreTokens in it
30
+
while(st.hasMoreTokens())
31
+
{
32
+
// This temporarily stores the next Token in the StringTokenizer
33
+
day = st.nextToken();
34
+
// This uses the isValidDate method to make sure the Token has 8 digits
35
+
if(isValidDate(day))
36
+
{
37
+
// If isValidDate is true, the day is stored at the index of the array, and the index increases by 1;
38
+
dates[index] = day;
39
+
index++;
40
+
}
41
+
else
42
+
{
43
+
// If isValidDate is false, the day is printed to the console and not stored in the array
44
+
System.out.println(day + " is not a valid date, and was not put in the array.");
45
+
}
46
+
}
47
+
// This gets the next line of the file and stores it
48
+
line = tfi.readLine();
49
+
}
50
+
// If this test is true, it means the exit condition for the while loop was index was not less than the length of the array, which means not all dates were stored in the array
51
+
if(line != null)
52
+
{
53
+
System.out.println("There were more dates than space in the array.");
54
+
System.out.println("The array can only store " + dates.length + " dates.");
0 commit comments